Flesh out the create API and wire everything up together. Next up, testing.
This commit is contained in:
parent
2afb8c85b1
commit
9b9a29c310
10 changed files with 156 additions and 15 deletions
34
data/userfiles.py
Normal file
34
data/userfiles.py
Normal file
|
@ -0,0 +1,34 @@
|
|||
import boto
|
||||
import os
|
||||
|
||||
from boto.s3.key import Key
|
||||
from uuid import uuid4
|
||||
|
||||
|
||||
class S3FileWriteException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class UserRequestFiles(object):
|
||||
def __init__(self, s3_access_key, s3_secret_key, bucket_name):
|
||||
self._s3_conn = boto.s3.connection.S3Connection(s3_access_key,
|
||||
s3_secret_key,
|
||||
is_secure=False)
|
||||
self._bucket = self._s3_conn.get_bucket(bucket_name)
|
||||
self._prefix = 'userfiles'
|
||||
|
||||
def store_file(self, flask_file):
|
||||
file_id = str(uuid4())
|
||||
full_key = os.path.join(self._prefix, file_id)
|
||||
k = Key(full_key)
|
||||
bytes_written = k.set_contents_from_file(flask_file)
|
||||
|
||||
if bytes_written == 0:
|
||||
raise S3FileWriteException('Unable to write file to S3')
|
||||
|
||||
return file_id
|
||||
|
||||
def get_file_url(self, file_id, expires_in=300):
|
||||
full_key = os.path.join(self._prefix, file_id)
|
||||
k = Key(full_key)
|
||||
return k.generate_url(expires_in)
|
Reference in a new issue