Can you list all the folders in the S3 bucket?

I have a bucket containing several folders, each folder contains several images. Is it possible to list all folders without repeating all the keys (folders and images) in the bucket. I use Python and boto.

+4
source share
2 answers

You can use list () with an empty prefix (first parameter) and a folder separator (second parameter) to achieve what you are asking for:

s3conn = boto.connect_s3(access_key, secret_key, security_token=token)
bucket = s3conn.get_bucket(bucket_name)
folders = bucket.list('', '/')
for folder in folders:
    print folder.name

Note:
There is no such thing as “folders” in S3. All you have is buckets and objects.

. : name-of-folder/name-of-file, , : name-of-file, : name-of-folder - , "".

AWS CLI ( ): s3ls <bucket-name> "" .

+2

Source: https://habr.com/ru/post/1542417/


All Articles