One way to see the contents would be:
for my_bucket_object in my_bucket.objects.all():
print(my_bucket_object)
Answer from garnaat on Stack OverflowOne way to see the contents would be:
for my_bucket_object in my_bucket.objects.all():
print(my_bucket_object)
This is similar to an 'ls' but it does not take into account the prefix folder convention and will list the objects in the bucket. It's left up to the reader to filter out prefixes which are part of the Key name.
In Python 2:
from boto.s3.connection import S3Connection
conn = S3Connection() # assumes boto.cfg setup
bucket = conn.get_bucket('bucket_name')
for obj in bucket.get_all_keys():
print(obj.key)
In Python 3:
from boto3 import client
conn = client('s3') # again assumes boto.cfg setup, assume AWS S3
for key in conn.list_objects(Bucket='bucket_name')['Contents']:
print(key['Key'])
listing the top level contents of a s3 bucket with Prefix and Delimiter
Using boto3 to list only all key names of files in a bucket - best methods when the bucket is ridiculously big?
boto3 s3 in Python - how to search objects
boto3 to delete s3 buckets and their objects
Till now, to check the names or last_modified details of all objects in a bucket, what I did was the following:
import boto3
s3r = boto3.resource('s3', aws_access_key_id=access_key_id, aws_secret_access_key=access_key_secret)
bucket_items = list(s3r.Bucket('BucketName').objects.all())
However, this seems to be a terrible idea for handling it when the bucket gets too big. Running this on a machine with 8GB RAM threw a memory error!
I assume I have to be doing something wrong if only fetching the file keys is this difficult. Is the objects.all() method actually saving details of the file's contents into memory when I call it?