ezdazuzena
ezdazuzena

Reputation: 6770

boto s3 bucket object filter with prefix and delimiter

I have a bucket with the following key structure:

path/to/file1
path/to/file2
path/of/file3
path/of/file4

And I would like to be able to get the list of "folders" inside path. The actual use case has many "subfolders", so I need to filter the listing. Ideally, I only want to receive two entries: to and of.

Using boto3, I was expecting the two following calls being basically equal, i.e. that the listing of both yields the same result:

Using the bucket returned by the S3 resource

s3 = boto3.resouce('s3')
bucket = s3.Bucket('bucketname')
bucket.objects.filter(Prefix='path/', Delimiter='/').all()

and the underlying client

s3 = boto3.resouce('s3')
s3.meta.client.list_objects(Bucket='path', Prefix='', Delimiter='/')

However, the first returns an emtpy list, while the second returns a JSON with the CommonPrefixes key having the two entries.

Question: What do I miss?

Upvotes: 6

Views: 11990

Answers (1)

ezdazuzena
ezdazuzena

Reputation: 6770

from https://github.com/boto/boto3/issues/134#issuecomment-116766812

The reason that it is not included in the list of objects returned is that the values that you are expecting when you use the delimiter are prefixes (e.g. Europe/, North America) and prefixes do not map into the object resource interface.

Upvotes: 3

Related Questions