Try to use a 301 redirect to download directly from s3. Allow the s3 and userfiles to run without internet access.

This commit is contained in:
yackob03 2013-12-03 16:39:07 -08:00
parent 82229fd8c8
commit bf85013ef6
4 changed files with 50 additions and 14 deletions

View file

@ -4,11 +4,6 @@ import logging
from boto.s3.key import Key
from uuid import uuid4
import hmac
import time
import urllib
import base64
import sha
logger = logging.getLogger(__name__)
@ -20,15 +15,23 @@ class S3FileWriteException(Exception):
class UserRequestFiles(object):
def __init__(self, s3_access_key, s3_secret_key, bucket_name):
self._s3_conn = boto.connect_s3(s3_access_key, s3_secret_key)
self._initialized = False
self._bucket_name = bucket_name
self._bucket = self._s3_conn.get_bucket(bucket_name)
self._access_key = s3_access_key
self._secret_key = s3_secret_key
self._prefix = 'userfiles'
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
def prepare_for_drop(self, mime_type):
""" Returns a signed URL to upload a file to our bucket. """
self._initialize_s3()
logger.debug('Requested upload url with content type: %s' % mime_type)
file_id = str(uuid4())
full_key = os.path.join(self._prefix, file_id)
@ -38,6 +41,7 @@ class UserRequestFiles(object):
return (url, file_id)
def store_file(self, flask_file):
self._initialize_s3()
file_id = str(uuid4())
full_key = os.path.join(self._prefix, file_id)
k = Key(self._bucket, full_key)
@ -51,6 +55,7 @@ class UserRequestFiles(object):
return file_id
def get_file_url(self, file_id, expires_in=300):
self._initialize_s3()
full_key = os.path.join(self._prefix, file_id)
k = Key(self._bucket, full_key)
return k.generate_url(expires_in)