bgneal@697: """s3.py bgneal@697: bgneal@697: This module provides the necessary S3 upload functionality for the user_photos bgneal@697: application. bgneal@697: bgneal@697: """ bgneal@697: from boto.s3.connection import S3Connection bgneal@697: from boto.s3.key import Key bgneal@697: bgneal@697: bgneal@697: class S3Bucket(object): bgneal@697: """This class abstracts an Amazon S3 bucket. bgneal@697: bgneal@697: We currently only need upload functionality. bgneal@697: bgneal@697: """ bgneal@697: def __init__(self, access_key, secret_key, bucket_name): bgneal@697: self.conn = S3Connection(access_key, secret_key) bgneal@697: self.bucket = self.conn.get_bucket(bucket_name, validate=False) bgneal@697: bgneal@697: def upload_from_filename(self, key_name, filename, metadata=None, bgneal@697: public=True): bgneal@697: """Uploads data from the file named by filename to a new key named bgneal@697: key_name. metadata, if not None, must be a dict of metadata key / value bgneal@697: pairs which will be added to the key. bgneal@697: bgneal@697: If public is True, the key will be made public after the upload. bgneal@697: bgneal@697: """ bgneal@697: key = self._make_key(key_name, metadata) bgneal@697: key.set_contents_from_filename(filename) bgneal@697: if public: bgneal@697: key.make_public() bgneal@697: bgneal@697: def upload_from_string(self, key_name, content, metadata=None, bgneal@697: public=True): bgneal@697: """Creates a new key with the given key_name, and uploads the string bgneal@697: content to it. metadata, if not None, must be a dict of metadata key / bgneal@697: value pairs which will be added to the key. bgneal@697: bgneal@697: If public is True, the key will be made public after the upload. bgneal@697: bgneal@697: """ bgneal@697: key = self._make_key(key_name, metadata) bgneal@697: key.set_contents_from_string(content) bgneal@697: if public: bgneal@697: key.make_public() bgneal@697: bgneal@697: def _make_key(self, key_name, metadata): bgneal@697: """Private method to create a key and optionally apply metadata to bgneal@697: it. bgneal@697: bgneal@697: """ bgneal@697: key = Key(self.bucket) bgneal@697: key.key = key_name bgneal@697: if metadata: bgneal@697: for k, v in metadata.iteritems(): bgneal@697: key.set_metadata(k, v) bgneal@697: return key