6

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.

nickponline
  • 25,354
  • 32
  • 99
  • 167
  • I think this is answered pretty well here: http://stackoverflow.com/questions/3337912/quick-way-to-list-all-files-in-amazon-s3-bucket – wubwubb May 29 '14 at 03:42

2 Answers2

8

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.

mgrandi
  • 3,389
  • 1
  • 18
  • 17
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
-2

Yes ! You can list by using prefix and delimiters of a key. Have a look at the following documentation.

    http://docs.aws.amazon.com/AmazonS3/latest/dev/ListingKeysHierarchy.html

yottabytt
  • 2,280
  • 1
  • 18
  • 14