2

According to this answer one can retrieve immediate "subdirectories" by querying by prefix and then obtaining CommonPrefix of the result of Client.list_objects() method.

Unfortunately, Client is a part of so-called "low level" API.

I am using different API:

session = Session(aws_access_key_id=access_key,
                  aws_secret_access_key=secret_key)

s3 = session.resource('s3')
my_bucket = s3.Bucket(bucket_name)
result = my_bucket.objects.filter(Prefix=prefix)

and this method does not return dictionary.

Is it possible to obtain common prefixes with higher level API in boto3?

Dims
  • 47,675
  • 117
  • 331
  • 600

2 Answers2

3

As noted in this answer, it seems that the Resource doesn't handle Delimiter well. It is often annoying, when your entire stack relies on Resource, to be told that, ah, you should have instantiated a Client instead...

Fortunately, a Resource object, such as your Bucket above, contains a client as well.

So, instead of the last line in your code sample, do:

paginator = my_bucket.meta.client.get_paginator('list_objects')
for resp in paginator.paginate(Bucket=my_bucket.name, Prefix=prefix, Delimiter='/', ...):
    for x in resp.get('CommonPrefixes', []):
        print(x['Prefix'])
Pierre D
  • 24,012
  • 7
  • 60
  • 96
1

You can access client from session.

session.client('s3').list_objects(Bucket=bucket_name, Prefix= prefix)
helloV
  • 50,176
  • 7
  • 137
  • 145