How to list objects by extension from s3 api?

前端 未结 6 674
醉话见心
醉话见心 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:38

    You can easily list all the elements by extension, getting all the elements (including folders) and then filtering by key.endswith('...')

    import boto3
    
    s3 = boto3.resource('s3')
    bucket = s3.Bucket('your-route')
    
    # Data from S3 is also filtered by endswith from key property
    for _ in bucket.objects.filter(Prefix=test_dir):
       if _.key.endswith('.zicu'):
          print('Value of object: ', _.key)
    

    In this case I'm filtering each element with a Prefix (test_dir) and then showing just the elements with .zicu extension

提交回复
热议问题