Skip to content

Commit

Permalink
adding serverside encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
pschrammel committed Jun 24, 2024
1 parent 3291c1b commit bf27d8f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
8 changes: 8 additions & 0 deletions thumbor_aws/result_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
"AWS Result Storage",
)

Config.define(
"AWS_RESULT_STORAGE_S3_SSE",
False,
"Use server side encryption for result storage.",
"AWS Result Storage",
)


class Storage(BaseStorage, S3Client):
def __init__(self, context):
Expand Down Expand Up @@ -143,6 +150,7 @@ async def put(self, image_bytes: bytes) -> str:
image_bytes,
content_type,
self.context.config.AWS_DEFAULT_LOCATION,
self.context.config.AWS_RESULT_STORAGE_S3_SSE,
)
logger.info(
"[RESULT_STORAGE] Image uploaded successfully to %s", file_abspath
Expand Down
8 changes: 5 additions & 3 deletions thumbor_aws/s3_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,23 @@ async def upload(
data: bytes,
content_type,
default_location,
encryption
) -> str:
"""Uploads a File to S3"""

async with self.get_client() as client:
response = None
try:
settings = {
"Bucket": self.bucket_name,
"Key": path,
"Body": data,
"ContentType": content_type,
"ContentType": content_type
}
if self.file_acl is not None:
settings["ACL"] = self.file_acl

if encryption:
settings["ServerSideEncryption"] = "AES256"

response = await client.put_object(**settings)
except Exception as error:
msg = f"Unable to upload image to {path}: {error} ({type(error)})"
Expand Down
11 changes: 11 additions & 0 deletions thumbor_aws/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@
"AWS Storage",
)

Config.define(
"AWS_STORAGE_S3_SSE",
False,
"Use server side encryption for storage.",
"AWS Storage",
)



class Storage(storages.BaseStorage, S3Client):
def __init__(self, context):
Expand Down Expand Up @@ -100,6 +108,7 @@ async def put(self, path: str, file_bytes: bytes) -> str:
file_bytes,
content_type,
self.context.config.AWS_DEFAULT_LOCATION,
self.context.config.AWS_STORAGE_S3_SSE,
)
return path

Expand All @@ -121,6 +130,7 @@ async def put_crypto(self, path: str) -> str:
key,
"application/text",
self.context.config.AWS_DEFAULT_LOCATION,
self.context.config.AWS_STORAGE_S3_SSE,
)

logger.debug("Stored crypto at %s", crypto_path)
Expand All @@ -136,6 +146,7 @@ async def put_detector_data(self, path: str, data: Any) -> str:
details,
"application/json",
self.context.config.AWS_DEFAULT_LOCATION,
self.context.config.AWS_STORAGE_S3_SSE,
)

async def get(self, path: str) -> bytes:
Expand Down

0 comments on commit bf27d8f

Please sign in to comment.