List bucket items

List all items in an S3 bucket

The following function can be used to list all items in an S3 bucket

import boto3

def list_bucket(bucket):
    client = boto3.client('s3')
    bucket_content = client.list_objects(Bucket=bucket)
    # check if S3-bucket is empty
    if bucket_content.get('Contents') == None:
        print('This S3-bucket is empty')
    else:
        # list all items in the S3 bucket
        for entry in bucket_content.get('Contents'):
            print(entry.get('Key'))

When calling the function you need to pass

  • bucket - the name of the bucket

bucket = 'bucket_name'

# list bucket items
list_bucket(bucket=bucket)

Last updated

Was this helpful?