This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/data/userfiles.py

57 lines
1.7 KiB
Python
Raw Normal View History

import boto
import os
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__)
class S3FileWriteException(Exception):
pass
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._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'
2013-10-26 22:37:53 +00:00
def prepare_for_drop(self, mime_type):
""" Returns a signed URL to upload a file to our bucket. """
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)
url = k.generate_url(300, 'PUT', headers={'Content-Type': mime_type},
encrypt_key=True)
return (url, file_id)
def store_file(self, flask_file):
file_id = str(uuid4())
full_key = os.path.join(self._prefix, file_id)
k = Key(self._bucket, full_key)
logger.debug('Setting s3 content type to: %s' % flask_file.content_type)
k.set_metadata('Content-Type', flask_file.content_type)
bytes_written = k.set_contents_from_file(flask_file, encrypt_key=True)
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(self._bucket, full_key)
return k.generate_url(expires_in)