1

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:

  1. 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?
  2. 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.
Matt Seymour
  • 8,880
  • 7
  • 60
  • 101
Jimmy
  • 12,087
  • 28
  • 102
  • 192

1 Answers1

2

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:

def read_file(self, key_name):
    k = Key(self.__bucket)
    k.key = key_name
    if k.exists():
        rawData = k.read()
    else :
        rawData = None
    return rawData
Avia
  • 1,829
  • 2
  • 14
  • 15
  • Thank you for the help. I'm still a little confused though. Firstly, if my "directory" that I am interested is here: /Images/FamilyPhotos/ would my prefix be "FamilyPhotos" or "/Images/FamilyPhotos/"? – Jimmy Dec 19 '12 at 13:21
  • Add the entire prefix "/Images/FamilyPhotos/" – Avia Dec 19 '12 at 14:29