-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #909 from weni-ai/feature/store-xlsx-logs-file
Add: Temp storage for logs
- Loading branch information
Showing
3 changed files
with
84 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from abc import ABC, abstractmethod | ||
from dataclasses import dataclass | ||
|
||
|
||
@dataclass | ||
class FileResponseDTO: | ||
status: int = None | ||
file_url: str = None | ||
err: str = None | ||
file_name: str = None | ||
|
||
|
||
class FileDataBase(ABC): | ||
|
||
@abstractmethod | ||
def add_file(file) -> FileResponseDTO: | ||
... |
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
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import uuid | ||
import boto3 | ||
from os.path import basename | ||
from django.conf import settings | ||
|
||
from .dto import FileDataBase, FileResponseDTO | ||
|
||
|
||
class s3FileDatabase(FileDataBase): | ||
def __init__(self) -> None: | ||
self.s3_client = boto3.client( | ||
's3', | ||
aws_access_key_id=settings.AWS_ACCESS_KEY_ID, | ||
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY, | ||
region_name=settings.AWS_REGION_NAME | ||
) | ||
|
||
def add_file(self, file) -> FileResponseDTO: | ||
file_name = basename("export_logs.xlsx") | ||
name, extension = file_name.split(".") | ||
file_name = f"{name}-{uuid.uuid4()}.{extension}" | ||
response = FileResponseDTO() | ||
try: | ||
self.s3_client.upload_fileobj(file, settings.AWS_BUCKET_NAME, file_name) | ||
response.status = 0 | ||
response.file_url = f"https://{settings.AWS_BUCKET_NAME}.s3.{settings.AWS_REGION_NAME}.amazonaws.com/{file_name}" | ||
response.file_name = file_name | ||
except Exception as exception: | ||
response.status = 1 | ||
response.err = str(exception) | ||
return response | ||
|
||
def create_presigned_url( | ||
self, | ||
file_name: str, | ||
expiration: int = 3600 | ||
) -> str: | ||
|
||
return self.s3_client.generate_presigned_url( | ||
'get_object', | ||
Params={ | ||
'Bucket': settings.AWS_BUCKET_NAME, | ||
'Key': file_name | ||
}, | ||
ExpiresIn=expiration | ||
) |