How to list objects by extension from s3 api?

前端 未结 6 807
醉话见心
醉话见心 2021-01-11 16:08

Can i somehow search objects in S3 by extension, not only by prefix?

Here is what i have now:

ListObjectsResponse r = s3Client.ListObjects(new Amazon         


        
6条回答
  •  半阙折子戏
    2021-01-11 16:19

    Because by using boto3 resource to get objects from S3, you can get satisfied result by using the returned file extension to filter what you want. Like this:

    import boto3
    s3 = boto3.resource('s3')
    my_bucket = s3.Bucket('my_bucket')
    files = my_bucket.objects.all()
    file_list = []
    for file in files:
        if file.key.endswith('.docx'):
             file_list.append(file.key)
    

    You can change the endswith string with what you want.

提交回复
热议问题