mirror of
https://github.com/matrix-org/synapse-s3-storage-provider.git
synced 2024-10-23 07:29:40 +00:00
Merge pull request #4 from matrix-org/erikj/allow_specifying_storage_class
Add storage class config option
This commit is contained in:
commit
86f5f6390f
1 changed files with 22 additions and 9 deletions
|
@ -31,6 +31,10 @@ import os
|
||||||
logger = logging.getLogger("synapse.s3")
|
logger = logging.getLogger("synapse.s3")
|
||||||
|
|
||||||
|
|
||||||
|
# The list of valid AWS storage class names
|
||||||
|
_VALID_STORAGE_CLASSES = ('STANDARD', 'REDUCED_REDUNDANCY', 'STANDARD_IA')
|
||||||
|
|
||||||
|
|
||||||
class S3StorageProviderBackend(StorageProvider):
|
class S3StorageProviderBackend(StorageProvider):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
|
@ -40,16 +44,18 @@ class S3StorageProviderBackend(StorageProvider):
|
||||||
|
|
||||||
def __init__(self, hs, config):
|
def __init__(self, hs, config):
|
||||||
self.cache_directory = hs.config.media_store_path
|
self.cache_directory = hs.config.media_store_path
|
||||||
self.bucket = config
|
self.bucket = config["bucket"]
|
||||||
|
self.storage_class = config["storage_class"]
|
||||||
|
|
||||||
def store_file(self, path, file_info):
|
def store_file(self, path, file_info):
|
||||||
"""See StorageProvider.store_file"""
|
"""See StorageProvider.store_file"""
|
||||||
|
|
||||||
def _store_file():
|
def _store_file():
|
||||||
with open(os.path.join(self.cache_directory, path), 'rb') as f:
|
boto3.resource('s3').Bucket(self.bucket).upload_file(
|
||||||
boto3.resource('s3').Bucket(self.bucket).put_object(
|
Filename=os.path.join(self.cache_directory, path),
|
||||||
Key=path, Body=f,
|
Key=path,
|
||||||
)
|
ExtraArgs={"StorageClass": self.storage_class},
|
||||||
|
)
|
||||||
|
|
||||||
return make_deferred_yieldable(
|
return make_deferred_yieldable(
|
||||||
reactor.callInThread(_store_file)
|
reactor.callInThread(_store_file)
|
||||||
|
@ -68,11 +74,18 @@ class S3StorageProviderBackend(StorageProvider):
|
||||||
|
|
||||||
The returned value is passed into the constructor.
|
The returned value is passed into the constructor.
|
||||||
|
|
||||||
In this case we only care about a single param, the bucket, so lets
|
In this case we return a dict with fields, `bucket` and `storage_class`
|
||||||
just pull that out.
|
|
||||||
"""
|
"""
|
||||||
assert isinstance(config["bucket"], basestring)
|
bucket = config["bucket"]
|
||||||
return config["bucket"]
|
storage_class = config.get("storage_class", "STANDARD")
|
||||||
|
|
||||||
|
assert isinstance(bucket, basestring)
|
||||||
|
assert storage_class in _VALID_STORAGE_CLASSES
|
||||||
|
|
||||||
|
return {
|
||||||
|
"bucket": bucket,
|
||||||
|
"storage_class": storage_class,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class _S3DownloadThread(threading.Thread):
|
class _S3DownloadThread(threading.Thread):
|
||||||
|
|
Loading…
Reference in a new issue