Can you list all folders in an S3 bucket?

amazon-s3, boto, python

Solution

You can use list() with an empty prefix (first parameter) and a folder delimiter (second parameter) to achieve what you're 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

Remark: In S3 there is no such thing as "folders". All you have is buckets and objects.

The objects represent files. When you name a file: `name-of-folder/name-of-file` it will look as if it's a file: `name-of-file` that resides inside folder: `name-of-folder` - but in reality there's no such thing as the "folder".

You can also use AWS CLI (Command Line Interface): the command `s3ls <bucket-name>` will list only the "folders" in the first-level of the bucket.

Problem

I have a bucket containing a number of folders each folders contains a number of images. Is it possible to list all the folders without iterating through all keys (folders and images) in the bucket. I'm using Python and boto.

Original source

Related problems