Listing contents of a bucket with boto3
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island
--
Chapters
00:00 Question
00:29 Accepted answer (Score 386)
00:43 Answer 2 (Score 159)
01:20 Answer 3 (Score 95)
01:36 Answer 4 (Score 79)
02:30 Thank you
--
Full question
https://stackoverflow.com/questions/3024...
Answer 3 links:
[s3 ]: https://gist.github.com/seansummers/b2bf...
[S3 guarantees UTF-8 binary sorted results]: https://docs.aws.amazon.com/AmazonS3/lat...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #amazonwebservices #amazons3 #boto3 #boto
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island
--
Chapters
00:00 Question
00:29 Accepted answer (Score 386)
00:43 Answer 2 (Score 159)
01:20 Answer 3 (Score 95)
01:36 Answer 4 (Score 79)
02:30 Thank you
--
Full question
https://stackoverflow.com/questions/3024...
Answer 3 links:
[s3 ]: https://gist.github.com/seansummers/b2bf...
[S3 guarantees UTF-8 binary sorted results]: https://docs.aws.amazon.com/AmazonS3/lat...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #amazonwebservices #amazons3 #boto3 #boto
#avk47
ACCEPTED ANSWER
Score 405
One way to see the contents would be:
for my_bucket_object in my_bucket.objects.all():
print(my_bucket_object)
ANSWER 2
Score 165
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'])
ANSWER 3
Score 98
I'm assuming you have configured authentication separately.
import boto3
s3 = boto3.resource('s3')
my_bucket = s3.Bucket('bucket_name')
for file in my_bucket.objects.all():
print(file.key)
ANSWER 4
Score 57
In order to handle large key listings (i.e. when the directory list is greater than 1000 items), I used the following code to accumulate key values (i.e. filenames) with multiple listings (thanks to Amelio above for the first lines). Code is for python3:
from boto3 import client
bucket_name = "my_bucket"
prefix = "my_key/sub_key/lots_o_files"
s3_conn = client('s3') # type: BaseClient ## again assumes boto.cfg setup, assume AWS S3
s3_result = s3_conn.list_objects_v2(Bucket=bucket_name, Prefix=prefix, Delimiter = "/")
if 'Contents' not in s3_result:
#print(s3_result)
return []
file_list = []
for key in s3_result['Contents']:
file_list.append(key['Key'])
print(f"List count = {len(file_list)}")
while s3_result['IsTruncated']:
continuation_key = s3_result['NextContinuationToken']
s3_result = s3_conn.list_objects_v2(Bucket=bucket_name, Prefix=prefix, Delimiter="/", ContinuationToken=continuation_key)
for key in s3_result['Contents']:
file_list.append(key['Key'])
print(f"List count = {len(file_list)}")
return file_list