Skip to content
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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions src/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand Down Expand Up @@ -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
)
Comment on lines +98 to +104
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Глобальный конфиг S3


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
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Необходимые данные.


file_stream = io.BytesIO(file_content)
Copy link
Owner Author

Choose a reason for hiding this comment

The 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
Copy link
Owner Author

Choose a reason for hiding this comment

The 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
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Сохраняем данные в бд


return new_file_name
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тестовый ответ



class UserManager:
def __init__(self):
Expand Down