maltman maltman - 11 months ago 85
Python Question

Boto3 get contents of S3 bucket

I am trying to get the list of files in a subfolder of a bucket. Everything works fine, except, when I try to parse the files I notice that the first key my code pulls is the subfolder name. Is there any way to leave out the subfolder name as a key?

s3 = session.resource('s3')
bucket = s3.Bucket('bucket_name')

for obj in bucket.objects.filter(Prefix="sub1"):
key = obj.key

Results from print key


Answer Source

I'd imagine there is a .exclude on the collections like in django but i couldn't see it as an option. You could try something like this

s3 = session.resource('s3')
bucket = s3.Bucket('bucket_name')
prefix = "sub1/"
data = [obj for obj in list(bucket.objects.filter(Prefix=prefix)) if obj.key != prefix]

for obj in data: