diff --git a/oyster/storage/s3.py b/oyster/storage/s3.py index d47901a..0228ec8 100644 --- a/oyster/storage/s3.py +++ b/oyster/storage/s3.py @@ -7,6 +7,7 @@ class S3Storage(object): storage_type = 's3' def __init__(self, kernel): + self.kernel = kernel self.s3conn = boto.connect_s3(settings.AWS_KEY, settings.AWS_SECRET) self._bucket = False @@ -16,17 +17,24 @@ class S3Storage(object): self._bucket = self.s3conn.create_bucket(settings.AWS_BUCKET) return self._bucket + def _get_opt(self, doc_class, setting, default=None): + """ doc_class first, then setting, then default """ + self.kernel.doc_classes[doc_class].get(setting, + getattr(settings, setting, default)) + def put(self, tracked_doc, data, content_type): """ upload the document to S3 """ k = boto.s3.key.Key(self.bucket) - key_name = getattr(settings, 'AWS_PREFIX', '') + tracked_doc['_id'] + aws_prefix = self._get_opt(tracked_doc['doc_class'], 'AWS_PREFIX', '') + aws_bucket = self._get_opt(tracked_doc['doc_class'], 'AWS_BUCKET') + key_name = aws_prefix + tracked_doc['_id'] k.key = key_name headers = {'x-amz-acl': 'public-read', 'Content-Type': content_type} k.set_contents_from_string(data, headers=headers) # can also set metadata if we want, useful? - url = 'http://%s.s3.amazonaws.com/%s' % (settings.AWS_BUCKET, key_name) + url = 'http://%s.s3.amazonaws.com/%s' % (aws_bucket, key_name) return url def get(self, id):