简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM