maltman maltman - 3 months ago 18
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
print(key)


Results from print key

sub1/
sub1/file1
sub1/file2
.
.

Answer

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:
    print(obj.key)