Retrieving subfolders names in S3 bucket from boto3

Below piece of code returns ONLY the ‘subfolders’ in a ‘folder’ from s3 bucket.

import boto3
bucket="my-bucket"
#Make sure you provide / in the end
prefix = 'prefix-name-with-slash/'  

client = boto3.client('s3')
result = client.list_objects(Bucket=bucket, Prefix=prefix, Delimiter="https://stackoverflow.com/")
for o in result.get('CommonPrefixes'):
    print 'sub folder : ', o.get('Prefix')

For more details, you can refer to https://github.com/boto/boto3/issues/134

Leave a Comment