Skip to content

Commit

Permalink
DH-4603 Decrypt file content if it is encrypted
Browse files Browse the repository at this point in the history
  • Loading branch information
jcjc712 committed Sep 19, 2023
1 parent 733eaea commit 1bb8aff
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions dataherald/utils/s3.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import boto3
from cryptography.fernet import InvalidToken

from dataherald.config import Settings
from dataherald.utils.encrypt import FernetEncrypt


class S3:
def __init__(self):
self.settings = Settings()

def download(self, path: str) -> str:
fernet_encrypt = FernetEncrypt()
path = path.split("/")
s3_client = boto3.client(
"s3",
aws_access_key_id=self.settings.s3_aws_access_key_id,
aws_secret_access_key=self.settings.s3_aws_secret_access_key,
)

file_location = f"tmp/{path[-1]}"
s3_client.download_file(
Bucket=path[2], Key=f"{path[-1]}", Filename=f"tmp/{path[-1]}"
Bucket=path[2], Key=f"{path[-1]}", Filename=file_location
)
return f"tmp/{path[-1]}"
# Decrypt file content if it is encrypted
try:
with open(file_location) as file_object:
decrypted_content = fernet_encrypt.decrypt(file_object.read())
with open(file_location, "w") as file_object:
file_object.write(decrypted_content)
except InvalidToken:
pass

return file_location

0 comments on commit 1bb8aff

Please sign in to comment.