listing the top level contents of a s3 bucket with Prefix and Delimiter
See original GitHub issueApologies for what sounds like a very basic question. In this example from the s3 docs is there a way to list the continents? I was hoping this might work, but it doesn’t seem to:
import boto3
s3 = boto3.resource('s3')
bucket = s3.Bucket('edsu-test-bucket')
for o in bucket.objects.filter(Delimiter='/'):
print(o.key)
However, the equivalent code using boto2 does seem to work the way I expect:
import boto
s3 = boto.connect_s3()
bucket = s3.get_bucket('edsu-test-bucket')
for o in bucket.list(delimiter='/'):
print(o.name)
Issue Analytics
- State:
- Created 8 years ago
- Reactions:13
- Comments:16 (2 by maintainers)
Top Results From Across the Web
Organizing objects using prefixes - AWS Documentation
You can use prefixes to organize the data that you store in Amazon S3 buckets. A prefix is a string of characters at...
Read more >Retrieving subfolders names in S3 bucket from boto3
A reminder about boto3 : boto3.resource is a nice high level API. ... a simple way to list contents of a subfolder in...
Read more >List files and folders of AWS S3 bucket using prefix & delimiter
It is used to get all the objects of the specified bucket. The arguments prefix and delimiter for this method is used for...
Read more >Quickest Ways to List Files in S3 Bucket - Binary Guy
As you can see it is easy to list files from one folder by using the “Prefix” parameter. List files in S3 using...
Read more >Difference between prefixes and nested folders in Amazon S3
A key prefix is a string of characters that can be the complete path in front of the object name (including the bucket...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@edsu I ran into this as well. The “directories” to list aren’t really objects (but substrings of object keys), so I do not expect them to show up in an objects collection. As a quick workaround, I list them via
client.list_objects
.For non-public buckets (or buckets that you can explicitly access):
Would print:
Europe/
North America/
This doesn’t support anonymous calls, though. When I run this, I get an
AccessDenied
error.For anonymous calls I haven’t found a way to use a s3 resource at all (so far). But I can call
list_objects
on a low-level client:Not very beautiful, but it prints what I wanted
North America/Canada/
North America/USA/
@amatthies is on the right track here. The reason that it is not included in the list of objects returned is that the values that you are expecting when you use the delimiter are prefixes (e.g.
Europe/
,North America
) and prefixes do not map into the object resource interface. If you want to know the prefixes of the objects in a bucket you will have to uselist_objects
. However, I would suggest to use the pagination interface for this because this will allow you to iterate through all objects in the bucket without having to provide pagination tokens:As to your question as how to use anonymous clients for resources try the following. You will have to hook into the event system to disable signing:
I realize that this not documented anywhere. Documenting the event system and things you can do with it is on our list of thing that we want to do.
Based on the conversation, I see the following action items:
Let me know what you all think or if there is anything else that should be added to this list.