2013-10-25 05:14:38 +00:00
|
|
|
import boto
|
|
|
|
import os
|
2013-10-25 22:18:06 +00:00
|
|
|
import logging
|
2013-10-25 05:14:38 +00:00
|
|
|
|
|
|
|
from boto.s3.key import Key
|
|
|
|
from uuid import uuid4
|
|
|
|
|
|
|
|
|
2013-10-25 22:18:06 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2013-10-25 05:14:38 +00:00
|
|
|
class S3FileWriteException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class UserRequestFiles(object):
|
|
|
|
def __init__(self, s3_access_key, s3_secret_key, bucket_name):
|
2013-12-04 00:39:07 +00:00
|
|
|
self._initialized = False
|
2013-10-26 21:20:59 +00:00
|
|
|
self._bucket_name = bucket_name
|
|
|
|
self._access_key = s3_access_key
|
|
|
|
self._secret_key = s3_secret_key
|
2013-10-25 05:14:38 +00:00
|
|
|
self._prefix = 'userfiles'
|
2013-12-04 00:39:07 +00:00
|
|
|
self._s3_conn = None
|
|
|
|
self._bucket = None
|
|
|
|
|
|
|
|
def _initialize_s3(self):
|
|
|
|
if not self._initialized:
|
|
|
|
self._s3_conn = boto.connect_s3(self._access_key, self._secret_key)
|
|
|
|
self._bucket = self._s3_conn.get_bucket(self._bucket_name)
|
|
|
|
self._initialized = True
|
2013-10-25 05:14:38 +00:00
|
|
|
|
2013-10-26 22:37:53 +00:00
|
|
|
def prepare_for_drop(self, mime_type):
|
2013-10-26 21:20:59 +00:00
|
|
|
""" Returns a signed URL to upload a file to our bucket. """
|
2013-12-04 00:39:07 +00:00
|
|
|
self._initialize_s3()
|
2013-10-26 22:37:53 +00:00
|
|
|
logger.debug('Requested upload url with content type: %s' % mime_type)
|
|
|
|
file_id = str(uuid4())
|
|
|
|
full_key = os.path.join(self._prefix, file_id)
|
|
|
|
k = Key(self._bucket, full_key)
|
2013-10-31 15:32:08 +00:00
|
|
|
url = k.generate_url(300, 'PUT', headers={'Content-Type': mime_type},
|
|
|
|
encrypt_key=True)
|
2013-10-26 21:20:59 +00:00
|
|
|
return (url, file_id)
|
|
|
|
|
2014-02-18 23:09:14 +00:00
|
|
|
def store_file(self, file_like_obj, content_type):
|
2013-12-04 00:39:07 +00:00
|
|
|
self._initialize_s3()
|
2013-10-25 05:14:38 +00:00
|
|
|
file_id = str(uuid4())
|
|
|
|
full_key = os.path.join(self._prefix, file_id)
|
2013-10-25 22:18:06 +00:00
|
|
|
k = Key(self._bucket, full_key)
|
2014-02-18 23:09:14 +00:00
|
|
|
logger.debug('Setting s3 content type to: %s' % content_type)
|
|
|
|
k.set_metadata('Content-Type', content_type)
|
|
|
|
bytes_written = k.set_contents_from_file(file_like_obj, encrypt_key=True,
|
|
|
|
rewind=True)
|
2013-10-25 05:14:38 +00:00
|
|
|
|
|
|
|
if bytes_written == 0:
|
|
|
|
raise S3FileWriteException('Unable to write file to S3')
|
|
|
|
|
|
|
|
return file_id
|
|
|
|
|
2014-04-01 04:23:53 +00:00
|
|
|
def get_file_url(self, file_id, expires_in=300, mime_type=None):
|
2013-12-04 00:39:07 +00:00
|
|
|
self._initialize_s3()
|
2013-10-25 05:14:38 +00:00
|
|
|
full_key = os.path.join(self._prefix, file_id)
|
2013-10-25 22:18:06 +00:00
|
|
|
k = Key(self._bucket, full_key)
|
2014-04-01 04:23:53 +00:00
|
|
|
headers = None
|
|
|
|
if mime_type:
|
|
|
|
headers={'Content-Type': mime_type}
|
|
|
|
|
|
|
|
return k.generate_url(expires_in, headers=headers)
|
2014-02-12 18:52:12 +00:00
|
|
|
|
|
|
|
def get_file_checksum(self, file_id):
|
|
|
|
self._initialize_s3()
|
|
|
|
full_key = os.path.join(self._prefix, file_id)
|
|
|
|
k = self._bucket.lookup(full_key)
|
|
|
|
return k.etag[1:-1][:7]
|