2013-09-25 21:50:03 +00:00
|
|
|
import cStringIO as StringIO
|
|
|
|
import os
|
2013-10-20 06:39:23 +00:00
|
|
|
import logging
|
2013-09-25 21:50:03 +00:00
|
|
|
|
|
|
|
import boto.s3.connection
|
2014-08-12 06:06:44 +00:00
|
|
|
import boto.gs.connection
|
2013-09-25 21:50:03 +00:00
|
|
|
import boto.s3.key
|
2014-08-12 06:06:44 +00:00
|
|
|
import boto.gs.key
|
2013-09-25 21:50:03 +00:00
|
|
|
|
2014-04-03 21:31:46 +00:00
|
|
|
from storage.basestorage import BaseStorage
|
2013-09-25 21:50:03 +00:00
|
|
|
|
|
|
|
|
2013-10-20 06:39:23 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class StreamReadKeyAsFile(object):
|
2013-10-31 15:32:08 +00:00
|
|
|
def __init__(self, key):
|
|
|
|
self._key = key
|
|
|
|
self._finished = False
|
2013-10-20 06:39:23 +00:00
|
|
|
|
2013-10-31 15:32:08 +00:00
|
|
|
def __enter__(self):
|
|
|
|
return self
|
2013-10-20 06:39:23 +00:00
|
|
|
|
2013-10-31 15:32:08 +00:00
|
|
|
def __exit__(self, type, value, tb):
|
|
|
|
self._key.close(fast=True)
|
2013-10-20 06:39:23 +00:00
|
|
|
|
2013-10-31 15:32:08 +00:00
|
|
|
def read(self, amt=None):
|
|
|
|
if self._finished:
|
|
|
|
return None
|
2013-10-20 06:39:23 +00:00
|
|
|
|
2013-10-31 15:32:08 +00:00
|
|
|
resp = self._key.read(amt)
|
|
|
|
if not resp:
|
|
|
|
self._finished = True
|
|
|
|
return resp
|
2013-10-20 06:39:23 +00:00
|
|
|
|
|
|
|
|
2014-08-12 06:06:44 +00:00
|
|
|
class _CloudStorage(BaseStorage):
|
|
|
|
def __init__(self, connection_class, key_class, upload_params, storage_path, access_key,
|
|
|
|
secret_key, bucket_name):
|
2013-12-04 00:39:07 +00:00
|
|
|
self._initialized = False
|
2014-08-12 06:06:44 +00:00
|
|
|
self._bucket_name = bucket_name
|
|
|
|
self._access_key = access_key
|
|
|
|
self._secret_key = secret_key
|
2013-10-31 15:32:08 +00:00
|
|
|
self._root_path = storage_path
|
2014-08-12 06:06:44 +00:00
|
|
|
self._connection_class = connection_class
|
|
|
|
self._key_class = key_class
|
|
|
|
self._upload_params = upload_params
|
|
|
|
self._cloud_conn = None
|
|
|
|
self._cloud_bucket = None
|
2013-12-04 00:39:07 +00:00
|
|
|
|
2014-08-12 06:06:44 +00:00
|
|
|
def _initialize_cloud_conn(self):
|
2013-12-04 00:39:07 +00:00
|
|
|
if not self._initialized:
|
2014-08-12 06:06:44 +00:00
|
|
|
self._cloud_conn = self._connection_class(self._access_key, self._secret_key)
|
|
|
|
self._cloud_bucket = self._cloud_conn.get_bucket(self._bucket_name)
|
2013-12-04 00:39:07 +00:00
|
|
|
self._initialized = True
|
2013-10-31 15:32:08 +00:00
|
|
|
|
|
|
|
def _debug_key(self, key):
|
|
|
|
"""Used for debugging only."""
|
|
|
|
orig_meth = key.bucket.connection.make_request
|
|
|
|
|
|
|
|
def new_meth(*args, **kwargs):
|
|
|
|
print '#' * 16
|
|
|
|
print args
|
|
|
|
print kwargs
|
|
|
|
print '#' * 16
|
|
|
|
return orig_meth(*args, **kwargs)
|
|
|
|
key.bucket.connection.make_request = new_meth
|
|
|
|
|
|
|
|
def _init_path(self, path=None):
|
|
|
|
path = os.path.join(self._root_path, path) if path else self._root_path
|
|
|
|
if path and path[0] == '/':
|
|
|
|
return path[1:]
|
|
|
|
return path
|
|
|
|
|
|
|
|
def get_content(self, path):
|
2014-08-12 06:06:44 +00:00
|
|
|
self._initialize_cloud_conn()
|
2013-10-31 15:32:08 +00:00
|
|
|
path = self._init_path(path)
|
2014-08-12 06:06:44 +00:00
|
|
|
key = self._key_class(self._cloud_bucket, path)
|
2013-10-31 15:32:08 +00:00
|
|
|
if not key.exists():
|
|
|
|
raise IOError('No such key: \'{0}\''.format(path))
|
|
|
|
return key.get_contents_as_string()
|
|
|
|
|
|
|
|
def put_content(self, path, content):
|
2014-08-12 06:06:44 +00:00
|
|
|
self._initialize_cloud_conn()
|
2013-10-31 15:32:08 +00:00
|
|
|
path = self._init_path(path)
|
2014-08-12 06:06:44 +00:00
|
|
|
key = self._key_class(self._cloud_bucket, path)
|
|
|
|
key.set_contents_from_string(content, **self._upload_params)
|
2013-10-31 15:32:08 +00:00
|
|
|
return path
|
|
|
|
|
2014-07-07 20:21:27 +00:00
|
|
|
def get_supports_resumeable_downloads(self):
|
2014-07-02 04:39:59 +00:00
|
|
|
return True
|
|
|
|
|
2013-12-04 00:39:07 +00:00
|
|
|
def get_direct_download_url(self, path, expires_in=60):
|
2014-08-12 06:06:44 +00:00
|
|
|
self._initialize_cloud_conn()
|
2013-12-04 00:39:07 +00:00
|
|
|
path = self._init_path(path)
|
2014-08-12 06:06:44 +00:00
|
|
|
k = self._key_class(self._cloud_bucket, path)
|
2013-12-04 00:39:07 +00:00
|
|
|
return k.generate_url(expires_in)
|
|
|
|
|
2013-10-31 15:32:08 +00:00
|
|
|
def stream_read(self, path):
|
2014-08-12 06:06:44 +00:00
|
|
|
self._initialize_cloud_conn()
|
2013-10-31 15:32:08 +00:00
|
|
|
path = self._init_path(path)
|
2014-08-12 06:06:44 +00:00
|
|
|
key = self._key_class(self._cloud_bucket, path)
|
2013-10-31 15:32:08 +00:00
|
|
|
if not key.exists():
|
|
|
|
raise IOError('No such key: \'{0}\''.format(path))
|
|
|
|
while True:
|
|
|
|
buf = key.read(self.buffer_size)
|
|
|
|
if not buf:
|
|
|
|
break
|
|
|
|
yield buf
|
|
|
|
|
|
|
|
def stream_read_file(self, path):
|
2014-08-12 06:06:44 +00:00
|
|
|
self._initialize_cloud_conn()
|
2013-10-31 15:32:08 +00:00
|
|
|
path = self._init_path(path)
|
2014-08-12 06:06:44 +00:00
|
|
|
key = self._key_class(self._cloud_bucket, path)
|
2013-10-31 15:32:08 +00:00
|
|
|
if not key.exists():
|
|
|
|
raise IOError('No such key: \'{0}\''.format(path))
|
|
|
|
return StreamReadKeyAsFile(key)
|
|
|
|
|
|
|
|
def stream_write(self, path, fp):
|
|
|
|
# Minimum size of upload part size on S3 is 5MB
|
2014-08-12 06:06:44 +00:00
|
|
|
self._initialize_cloud_conn()
|
2013-10-31 15:32:08 +00:00
|
|
|
buffer_size = 5 * 1024 * 1024
|
|
|
|
if self.buffer_size > buffer_size:
|
|
|
|
buffer_size = self.buffer_size
|
|
|
|
path = self._init_path(path)
|
2014-08-12 06:06:44 +00:00
|
|
|
mp = self._cloud_bucket.initiate_multipart_upload(path, **self._upload_params)
|
2013-10-31 15:32:08 +00:00
|
|
|
num_part = 1
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
buf = fp.read(buffer_size)
|
|
|
|
if not buf:
|
|
|
|
break
|
|
|
|
io = StringIO.StringIO(buf)
|
|
|
|
mp.upload_part_from_file(io, num_part)
|
|
|
|
num_part += 1
|
|
|
|
io.close()
|
|
|
|
except IOError:
|
|
|
|
break
|
|
|
|
mp.complete_upload()
|
|
|
|
|
|
|
|
def list_directory(self, path=None):
|
2014-08-12 06:06:44 +00:00
|
|
|
self._initialize_cloud_conn()
|
2013-10-31 15:32:08 +00:00
|
|
|
path = self._init_path(path)
|
|
|
|
if not path.endswith('/'):
|
|
|
|
path += '/'
|
|
|
|
ln = 0
|
|
|
|
if self._root_path != '/':
|
|
|
|
ln = len(self._root_path)
|
|
|
|
exists = False
|
2014-08-12 06:06:44 +00:00
|
|
|
for key in self._cloud_bucket.list(prefix=path, delimiter='/'):
|
2013-10-31 15:32:08 +00:00
|
|
|
exists = True
|
|
|
|
name = key.name
|
|
|
|
if name.endswith('/'):
|
|
|
|
yield name[ln:-1]
|
|
|
|
else:
|
|
|
|
yield name[ln:]
|
|
|
|
if exists is False:
|
|
|
|
# In order to be compliant with the LocalStorage API. Even though
|
|
|
|
# S3 does not have a concept of folders.
|
|
|
|
raise OSError('No such directory: \'{0}\''.format(path))
|
|
|
|
|
|
|
|
def exists(self, path):
|
2014-08-12 06:06:44 +00:00
|
|
|
self._initialize_cloud_conn()
|
2013-10-31 15:32:08 +00:00
|
|
|
path = self._init_path(path)
|
2014-08-12 06:06:44 +00:00
|
|
|
key = self._key_class(self._cloud_bucket, path)
|
2013-10-31 15:32:08 +00:00
|
|
|
return key.exists()
|
|
|
|
|
|
|
|
def remove(self, path):
|
2014-08-12 06:06:44 +00:00
|
|
|
self._initialize_cloud_conn()
|
2013-10-31 15:32:08 +00:00
|
|
|
path = self._init_path(path)
|
2014-08-12 06:06:44 +00:00
|
|
|
key = self._key_class(self._cloud_bucket, path)
|
2013-10-31 15:32:08 +00:00
|
|
|
if key.exists():
|
|
|
|
# It's a file
|
|
|
|
key.delete()
|
|
|
|
return
|
|
|
|
# We assume it's a directory
|
|
|
|
if not path.endswith('/'):
|
|
|
|
path += '/'
|
2014-08-12 06:06:44 +00:00
|
|
|
for key in self._cloud_bucket.list(prefix=path):
|
2013-10-31 15:32:08 +00:00
|
|
|
key.delete()
|
2014-08-12 06:06:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class S3Storage(_CloudStorage):
|
|
|
|
def __init__(self, storage_path, s3_access_key, s3_secret_key, s3_bucket):
|
|
|
|
upload_params = {
|
|
|
|
'encrypt_key': True,
|
|
|
|
}
|
|
|
|
super(S3Storage, self).__init__(boto.s3.connection.S3Connection, boto.s3.key.Key,
|
|
|
|
upload_params, storage_path, s3_access_key, s3_secret_key,
|
|
|
|
s3_bucket)
|
|
|
|
|
|
|
|
|
|
|
|
class GoogleCloudStorage(_CloudStorage):
|
|
|
|
def __init__(self, storage_path, access_key, secret_key, bucket_name):
|
|
|
|
super(GoogleCloudStorage, self).__init__(boto.gs.connection.GSConnection, boto.gs.key.Key, {},
|
|
|
|
storage_path, access_key, secret_key, bucket_name)
|
|
|
|
|
|
|
|
def stream_write(self, path, fp):
|
|
|
|
# Minimum size of upload part size on S3 is 5MB
|
|
|
|
self._initialize_cloud_conn()
|
|
|
|
path = self._init_path(path)
|
|
|
|
key = self._key_class(self._cloud_bucket, path)
|
|
|
|
key.set_contents_from_stream(fp)
|