2014-06-17 20:03:43 +00:00
|
|
|
import random
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from functools import wraps
|
|
|
|
|
2015-07-06 19:00:07 +00:00
|
|
|
from storage.basestorage import StoragePaths, BaseStorage, BaseStorageV2
|
2014-06-17 20:03:43 +00:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
def _location_aware(unbound_func):
|
|
|
|
@wraps(unbound_func)
|
|
|
|
def wrapper(self, locations, *args, **kwargs):
|
|
|
|
storage = None
|
|
|
|
for preferred in self.preferred_locations:
|
|
|
|
if preferred in locations:
|
|
|
|
storage = self._storages[preferred]
|
2015-10-26 23:06:05 +00:00
|
|
|
break
|
2014-06-17 20:03:43 +00:00
|
|
|
|
|
|
|
if not storage:
|
|
|
|
storage = self._storages[random.sample(locations, 1)[0]]
|
|
|
|
|
|
|
|
storage_func = getattr(storage, unbound_func.__name__)
|
|
|
|
return storage_func(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
class DistributedStorage(StoragePaths):
|
2016-08-24 16:55:33 +00:00
|
|
|
def __init__(self, storages, preferred_locations=None, default_locations=None, proxy=None):
|
2014-06-17 20:03:43 +00:00
|
|
|
self._storages = dict(storages)
|
2015-10-26 23:06:05 +00:00
|
|
|
self.preferred_locations = list(preferred_locations or [])
|
|
|
|
self.default_locations = list(default_locations or [])
|
2016-08-24 16:55:33 +00:00
|
|
|
self.proxy = proxy
|
2015-06-28 10:29:22 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def locations(self):
|
|
|
|
""" Returns the names of the locations supported. """
|
|
|
|
return list(self._storages.keys())
|
2014-06-17 20:03:43 +00:00
|
|
|
|
2016-08-24 16:55:33 +00:00
|
|
|
_get_direct_download_url = _location_aware(BaseStorage.get_direct_download_url)
|
|
|
|
|
2014-11-24 21:07:38 +00:00
|
|
|
get_direct_upload_url = _location_aware(BaseStorage.get_direct_upload_url)
|
2014-06-17 20:03:43 +00:00
|
|
|
get_content = _location_aware(BaseStorage.get_content)
|
|
|
|
put_content = _location_aware(BaseStorage.put_content)
|
|
|
|
stream_read = _location_aware(BaseStorage.stream_read)
|
|
|
|
stream_read_file = _location_aware(BaseStorage.stream_read_file)
|
|
|
|
stream_write = _location_aware(BaseStorage.stream_write)
|
|
|
|
exists = _location_aware(BaseStorage.exists)
|
|
|
|
remove = _location_aware(BaseStorage.remove)
|
2016-08-01 17:02:15 +00:00
|
|
|
validate = _location_aware(BaseStorage.validate)
|
2014-09-09 19:54:03 +00:00
|
|
|
get_checksum = _location_aware(BaseStorage.get_checksum)
|
|
|
|
get_supports_resumable_downloads = _location_aware(BaseStorage.get_supports_resumable_downloads)
|
2015-06-28 10:29:22 +00:00
|
|
|
|
2015-07-06 19:00:07 +00:00
|
|
|
initiate_chunked_upload = _location_aware(BaseStorageV2.initiate_chunked_upload)
|
|
|
|
stream_upload_chunk = _location_aware(BaseStorageV2.stream_upload_chunk)
|
|
|
|
complete_chunked_upload = _location_aware(BaseStorageV2.complete_chunked_upload)
|
2015-08-12 20:39:32 +00:00
|
|
|
cancel_chunked_upload = _location_aware(BaseStorageV2.cancel_chunked_upload)
|
2015-06-28 10:29:22 +00:00
|
|
|
|
2016-08-24 16:55:33 +00:00
|
|
|
|
2017-09-25 21:14:28 +00:00
|
|
|
def get_direct_download_url(self, locations, path, request_ip=None, expires_in=600, requires_cors=False,
|
2016-08-24 16:55:33 +00:00
|
|
|
head=False):
|
2017-09-25 21:14:28 +00:00
|
|
|
download_url = self._get_direct_download_url(locations, path, request_ip, expires_in, requires_cors, head)
|
2016-08-24 16:55:33 +00:00
|
|
|
if download_url is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if self.proxy is None:
|
|
|
|
return download_url
|
|
|
|
|
|
|
|
return self.proxy.proxy_download_url(download_url)
|
|
|
|
|
|
|
|
|
2015-06-28 10:29:22 +00:00
|
|
|
def copy_between(self, path, source_location, destination_location):
|
|
|
|
""" Copies a file between the source location and the destination location. """
|
|
|
|
source_storage = self._storages[source_location]
|
|
|
|
destination_storage = self._storages[destination_location]
|
|
|
|
source_storage.copy_to(destination_storage, path)
|