Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing contents of a bucket with boto3

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?

like image 384
Amelio Vazquez-Reina Avatar asked May 14 '15 23:05

Amelio Vazquez-Reina


People also ask

How do I view contents of S3 bucket?

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.

What is Boto3 client (' S3 ')?

​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.


2 Answers

One way to see the contents would be:

for my_bucket_object in my_bucket.objects.all():     print(my_bucket_object) 
like image 158
garnaat Avatar answered Oct 31 '22 04:10

garnaat


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']) 
like image 28
cgseller Avatar answered Oct 31 '22 04:10

cgseller