-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attachment (S3) #19
base: main
Are you sure you want to change the base?
Attachment (S3) #19
Changes from 1 commit
10716e8
af3cd0a
c5ce121
b4da7c0
f83ce76
1d01993
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,18 @@ | |
import pyotp | ||
import qrcode | ||
import io | ||
import os | ||
import aiohttp | ||
import uuid as uuid_generate | ||
|
||
from datetime import datetime | ||
from config import settings | ||
from api.models import UserStructure | ||
from packaging.version import Version as _versionCompare | ||
|
||
from api.models import UserStructure | ||
from src.db import Attachment, async_session_maker | ||
from src.s3 import _S3Connector, _S3Config | ||
from config import settings | ||
|
||
|
||
class TwoFactor: | ||
def __init__( | ||
|
@@ -90,6 +95,51 @@ def __init__( | |
self.result = result | ||
self.message = message | ||
|
||
S3Data = _S3Config( | ||
bucket_name=settings.S3_BUCKET_NAME, | ||
endpoint_url=settings.S3_ENDPOINT_URL, | ||
region_name=settings.S3_REGION_NAME, | ||
aws_access_key_id=settings.S3_ACCESS_KEY, | ||
aws_secret_access_key=settings.S3_SECRET_ACCESS_KEY | ||
) | ||
|
||
class AttachmentManager: | ||
def __init__(self): | ||
pass | ||
|
||
def file_url(self, file_name): | ||
return f'{S3Data.endpoint_url}/{S3Data.bucket_name}/{file_name}' | ||
|
||
@staticmethod | ||
async def upload( | ||
file_name: str, | ||
file_content: bytes | ||
): | ||
uuid = uuid_generate.uuid4() | ||
file_extension = os.path.splitext(file_name)[1].lstrip('.') | ||
new_file_name = f'{uuid}.{file_extension}' | ||
Comment on lines
+118
to
+120
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Необходимые данные. |
||
|
||
file_stream = io.BytesIO(file_content) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Открываем файл в двоичном режиме |
||
|
||
async with _S3Connector(S3Data) as s3: | ||
await s3.upload_fileobj(fileobj=file_stream, key=new_file_name) | ||
Comment on lines
+124
to
+125
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Загружаем файл на S3 |
||
|
||
attachment_data = Attachment( | ||
uuid=uuid, | ||
file_path=new_file_name, | ||
attachment_type='file', | ||
file_extension=file_extension | ||
) | ||
|
||
async with async_session_maker() as session: | ||
session.add(attachment_data) | ||
try: | ||
await session.commit() | ||
except Exception as e: | ||
await session.rollback() | ||
Comment on lines
+127
to
+139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Сохраняем данные в бд |
||
|
||
return new_file_name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тестовый ответ |
||
|
||
|
||
class UserManager: | ||
def __init__(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Глобальный конфиг S3