Search

Chapter 4. Listing files in available Amazon S3 buckets using notebook cells

download PDF

You can check the files available in buckets you have access to by listing the objects in the bucket. Because buckets use object storage rather than a typical file system, object naming works differently from normal file naming. Objects in a bucket are always known by a key, which consists of the full path in the bucket plus the name of the file itself.

Prerequisites

Procedure

  1. Create a new notebook cell and list the objects in the bucket. For example:

    bucket_name = 'std-user-bucket1'
    s3_client.list_objects_v2(Bucket=bucket_name)

    This returns several objects in the following format:

    {'Key': 'docker/registry/v2/blobs/sha256/00/0080913dd3f10aadb34asfgsgsdgasdga072049c93606b98bec84adb259b424f/data',
    'LastModified': datetime.datetime(2021, 4, 22, 1, 26, 1, tzinfo=tzlocal()),
    'ETag': '"6e02fad2deassadfsf900a4bd7344ffe"',
    'Size': 4052,
    'StorageClass': 'STANDARD'}
  2. You can make this list easier to read by printing only the key rather than the full response, for example:

    bucket_name = 'std-user-bucket1'
    for key in s3_client.list_objects_v2(Bucket=bucket_name)['Contents']:
        print(key['Key'])

    This returns output similar to the following:

    docker/registry/v2/blobs/sha256/00/0080913dd3f10aadb34asfgsgsdgasdga072049c93606b98bec84adb259b424f/data
  3. You can also filter your query to list for a specific "path" or file name, for example:

    bucket_name = 'std-user-bucket1'
    for key in s3_client.list_objects_v2(Bucket=bucket_name,Prefix='<start_of_file_path>')['Contents']:
        print(key['Key'])

    In the preceding example, replace <start_of_file_path> with your own value.

Red Hat logoGithubRedditYoutubeTwitter

Learn

Try, buy, & sell

Communities

About Red Hat Documentation

We help Red Hat users innovate and achieve their goals with our products and services with content they can trust.

Making open source more inclusive

Red Hat is committed to replacing problematic language in our code, documentation, and web properties. For more details, see the Red Hat Blog.

About Red Hat

We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.

© 2024 Red Hat, Inc.