Listing contents of a bucket with boto3

Amelio Vazquez-Reina picture Amelio Vazquez-Reina · May 15, 2015 · Viewed 320k times · Source

How can I see what's inside a bucket in S3 with boto3? (i.e. do an "ls")?

Doing the following:

import boto3
s3 = boto3.resource('s3')
my_bucket = s3.Bucket('some/path/')

returns:

s3.Bucket(name='some/path/')

How do I see its contents?

Answer

garnaat picture garnaat · May 15, 2015

One way to see the contents would be:

for my_bucket_object in my_bucket.objects.all():
    print(my_bucket_object)