3

I have

data = list(bucket.objects)

this will get me all of the objects in my aws s3 bucket
I want to be able to get only certain file types, such as a .png file.
i tried doing something like

data = list(bucket.objects.filter(Prefix = '*.png'))

but no luck. Any tips?

1
  • 1
    Prefix isn't going to work, because you are looking for the suffix, not the prefix. S3 doesn't index files by file extension, so you can't really do what you are asking without indexing the files via some other service. stackoverflow.com/questions/4709071/… Commented Oct 2, 2018 at 19:10

1 Answer 1

2

I guess the following should help:

client=boto3.client('s3',aws_access_key_id,aws_secret_access_key,region_name)
paginator = client.get_paginator('list_objects')

page_iterator_1 = paginator.paginate(Bucket='bucket-name',PaginationConfig={'PageSize':1})
page_iterator = page_iterator_1.search("Contents[?contains(Key,'png')].Key")

for page in page_iterator:
    print(page) # will contain the entire response
Sign up to request clarification or add additional context in comments.

1 Comment

page_iterator_1.search() happens client side unlike the filtering that happens using Prefix. For those looking for more information on that, see [official boto3 page] (boto3.amazonaws.com/v1/documentation/api/latest/guide/…) on filtering and JMESPath documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.