annotate core/s3.py @ 700:e888d627928f

Refactored the processing of image uploads. I suspect I will use this general algorithm in other places (like POTD), so I made it reusable.
author Brian Neal <bgneal@gmail.com>
date Wed, 11 Sep 2013 20:31:23 -0500
parents user_photos/s3.py@67f8d49a9377
children bf5340705d0c
rev   line source
bgneal@697 1 """s3.py
bgneal@697 2
bgneal@700 3 This module provides Amazon S3 convenience wrappers.
bgneal@697 4
bgneal@697 5 """
bgneal@697 6 from boto.s3.connection import S3Connection
bgneal@697 7 from boto.s3.key import Key
bgneal@697 8
bgneal@697 9
bgneal@697 10 class S3Bucket(object):
bgneal@697 11 """This class abstracts an Amazon S3 bucket.
bgneal@697 12
bgneal@700 13 We currently only support upload functionality.
bgneal@697 14
bgneal@697 15 """
bgneal@700 16 def __init__(self, access_key, secret_key, base_url, bucket_name):
bgneal@697 17 self.conn = S3Connection(access_key, secret_key)
bgneal@697 18 self.bucket = self.conn.get_bucket(bucket_name, validate=False)
bgneal@700 19 self.base_url = base_url
bgneal@700 20 if not base_url.endswith('/'):
bgneal@700 21 self.base_url += '/'
bgneal@700 22 self.name = bucket_name
bgneal@700 23
bgneal@700 24 def upload_from_file(self, key_name, fp, metadata=None, public=True):
bgneal@700 25 """Uploads data from the file object fp to a new key named
bgneal@700 26 key_name. metadata, if not None, must be a dict of metadata key / value
bgneal@700 27 pairs which will be added to the key.
bgneal@700 28
bgneal@700 29 If public is True, the key will be made public after the upload.
bgneal@700 30
bgneal@700 31 Returns the URL to the uploaded file.
bgneal@700 32
bgneal@700 33 """
bgneal@700 34 key = self._make_key(key_name, metadata)
bgneal@700 35 key.set_contents_from_file(fp)
bgneal@700 36 if public:
bgneal@700 37 key.make_public()
bgneal@700 38 return '{}{}/{}'.format(self.base_url, self.name, key_name)
bgneal@697 39
bgneal@697 40 def upload_from_filename(self, key_name, filename, metadata=None,
bgneal@697 41 public=True):
bgneal@697 42 """Uploads data from the file named by filename to a new key named
bgneal@697 43 key_name. metadata, if not None, must be a dict of metadata key / value
bgneal@697 44 pairs which will be added to the key.
bgneal@697 45
bgneal@697 46 If public is True, the key will be made public after the upload.
bgneal@697 47
bgneal@700 48 Returns the URL to the uploaded file.
bgneal@700 49
bgneal@697 50 """
bgneal@697 51 key = self._make_key(key_name, metadata)
bgneal@697 52 key.set_contents_from_filename(filename)
bgneal@697 53 if public:
bgneal@697 54 key.make_public()
bgneal@700 55 return '{}{}/{}'.format(self.base_url, self.name, key_name)
bgneal@697 56
bgneal@697 57 def upload_from_string(self, key_name, content, metadata=None,
bgneal@697 58 public=True):
bgneal@697 59 """Creates a new key with the given key_name, and uploads the string
bgneal@697 60 content to it. metadata, if not None, must be a dict of metadata key /
bgneal@697 61 value pairs which will be added to the key.
bgneal@697 62
bgneal@697 63 If public is True, the key will be made public after the upload.
bgneal@697 64
bgneal@700 65 Returns the URL to the uploaded file.
bgneal@700 66
bgneal@697 67 """
bgneal@697 68 key = self._make_key(key_name, metadata)
bgneal@697 69 key.set_contents_from_string(content)
bgneal@697 70 if public:
bgneal@697 71 key.make_public()
bgneal@700 72 return '{}{}/{}'.format(self.base_url, self.name, key_name)
bgneal@697 73
bgneal@697 74 def _make_key(self, key_name, metadata):
bgneal@697 75 """Private method to create a key and optionally apply metadata to
bgneal@697 76 it.
bgneal@697 77
bgneal@697 78 """
bgneal@697 79 key = Key(self.bucket)
bgneal@697 80 key.key = key_name
bgneal@697 81 if metadata:
bgneal@697 82 for k, v in metadata.iteritems():
bgneal@697 83 key.set_metadata(k, v)
bgneal@697 84 return key