-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DH-4603 Decrypt file content if it is encrypted
- Loading branch information
Showing
1 changed file
with
15 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |