cookiedough
cookiedough

Reputation: 3832

How to get objects from a folder in an S3 bucket

I am trying to traverse all objects inside a specific folder in my S3 bucket. The code I already have is like follows:

s3 = boto3.resource('s3')
bucket = s3.Bucket('bucket-name')
for obj in bucket.objects.filter(Prefix='folder/'):
    do_stuff(obj)

I need to use boto3.resource and not client. This code is not getting any objects at all although I have a bunch of text files in the folder. Can someone advise?

Upvotes: 4

Views: 3857

Answers (2)

stingMantis
stingMantis

Reputation: 340

I had to make sure to skip the first file. For some reason it thinks the folder name is the first file and that may not be what you want.

    for video_item in source_bucket.objects.filter(Prefix="my-folder-name/", Delimiter='/'):
    if video_item.key == 'my-folder-name/':
        continue
    do_something(video_item.key)

Upvotes: 1

Rockster
Rockster

Reputation: 66

Try adding the Delimiter attribute: Delimiter = '\' as you are filtering objects. The rest of the code looks fine.

Upvotes: 5

Related Questions