Reputation: 25914
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.
Upvotes: 7
Views: 27500
Reputation: 53525
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 s3 ls <bucket-name>
will list only the "folders" in the first-level of the bucket.
Upvotes: 8
Reputation: 2390
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
Upvotes: -2