Vingtoft
Vingtoft

Reputation: 14656

How to get all root folders of a S3 bucket

I have a AWS S3 bucket with the following top level content:

folder_1/
folder_2/
folder_3/
random_file.txt

All the folders have lots of files and folders inside them.

How can I write a boto3 script that retrieves only the names of the folders in the top level/root of the bucket?

I've been using the following approach:

bucket = s3.Bucket('mybucket')
root_folders = []
for key in bucket.objects.filter(Delimiter='/'):
    root_folders.append(key.key)

The result is (of course) just random_file.txt

The result Im looking for is:

['folder_1', 'folder_2', 'folder_3']

Upvotes: 0

Views: 2377

Answers (1)

spg
spg

Reputation: 9847

Using list_objects_v2, you can retrieve the folders in the CommonPrefixes field like this:

>>> s3.list_objects_v2(Bucket='mybucket', Delimiter='/')
{ ..., 
  u'CommonPrefixes': [
     {u'Prefix': 'folder_1/'}, 
     {u'Prefix': 'folder_2/'}, 
     {u'Prefix': 'folder_3/'}
   ]
 }

Upvotes: 2

Related Questions