If anyone has worked with boto I was wondering if you could help. I have this code currently:
from boto.s3.connection import S3Connection
from boto.s3.key import Key
conn = S3Connection(XXX, YYY)
bucket = conn.get_bucket('bucketname')
latest = max(bucket, key=lambda k: k.name)
latest.get_contents_to_filename()
I am confused about two things, and would really appreciate some help:
- Within my bucket I have created directories. How can I make it so this script only looks at a single directory within the bucket, i.e “Photos” not my entire S3 bucket?
- How to specify a download location. The script is designed to grab the latest datetime name’d file from an S3 bucket and download it, but I was wondering if I can specify where it is downloaded to.
It was hard for me as well but you need to understand that this is NOT a file system with directories.
To resolve your issue:
You can use the prefix parameter (from boto on GitHub):
:param prefix: allows you to limit the listing to a particular
prefix. For example, if you call the method with
prefix=’/foo/’ then the iterator will only cycle through
the keys that begin with the string ‘/foo/’.
https://github.com/boto/boto/blob/develop/boto/s3/bucket.py
Look at line 219
The code I am using for reading is something like: