Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get top-level folders in an S3 bucket using boto3?

I have an S3 bucket with a few top level folders, and hundreds of files in each of these folders. How do I get the names of these top level folders?

I have tried the following:

s3 = boto3.resource('s3', region_name='us-west-2', endpoint_url='https://s3.us-west-2.amazonaws.com')
bucket = s3.Bucket('XXX')

for obj in bucket.objects.filter(Prefix='', Delimiter='/'):
    print obj.key

But this doesn't seem to work. I have thought about using regex to filter all the folder names, but this doesn't seem time efficient.

Thanks in advance!

like image 646
Robert Bernard Avatar asked Dec 14 '22 12:12

Robert Bernard


1 Answers

Try this.

import boto3

client = boto3.client('s3')
paginator = client.get_paginator('list_objects')
result = paginator.paginate(Bucket='my-bucket', Delimiter='/')
for prefix in result.search('CommonPrefixes'):
    print(prefix.get('Prefix'))
like image 90
Shreyash Solanke Avatar answered Jan 05 '23 22:01

Shreyash Solanke