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?
To open the overview pane for an objectSign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that contains the object. In the Objects list, choose the name of the object for which you want an overview.
Boto3 is the official AWS SDK for Python, used to create, configure, and manage AWS services. The following are examples of defining a resource/client in boto3 for the Weka S3 service, managing credentials, and pre-signed URLs, generating secure temporary tokens, and using those to run S3 API calls.
One 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'])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With