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

Attachment (S3) #19

wants to merge 6 commits into from

Conversation

reques6e
Copy link
Owner

@reques6e reques6e commented Feb 5, 2025

Нужно сделать методы Attachment для API.

Схема работы загрузки вложений:

Отправка POST запроса на API -> Загрузка в S3 -> Сохранение данных в базе данных

Ответ от сервера:

{
    "message": "Вложение успешно создано",
    "data": {
        "uuid": "79b1b2f0-fe4f-4976-9145-e7c6da12e731",
        "url": "https://s3.depotmanager.repo.reques6e.ru/",
        "file_type": "image",
        "extension": "png"
    }
}

@reques6e
Copy link
Owner Author

reques6e commented Feb 5, 2025

Структура базы данных:

class Attachment(Base):
    __tablename__ = 'attachment'

    id = Column(Integer, primary_key=True, autoincrement=True)
    uuid = Column(String(100), nullable=False) # UUID вложения
    file_path = Column(String(250), nullable=False) # Путь к файлу (это не URL)
    attachment_type = Column(String(100), nullable=False) # Тип вложения: видео, фото, файл
    file_extension = Column(String(100), nullable=False) # Расширение файла

@@ -9,3 +9,4 @@ aiohttp==3.11.11
packaging==24.2
aioboto3==13.4.0
asyncmy==0.2.10
boto3==1.35.63
Copy link
Owner Author

Choose a reason for hiding this comment

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

В новой версии есть баг

boto/boto3#4400

'region_name': self.region_name,
'aws_access_key_id': self.aws_access_key_id,
'aws_secret_access_key': self.aws_secret_access_key,
'config': Config(signature_version='s3v4')
Copy link
Owner Author

Choose a reason for hiding this comment

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

Изменил сигнатуры на s3v4

Comment on lines +118 to +120
uuid = uuid_generate.uuid4()
file_extension = os.path.splitext(file_name)[1].lstrip('.')
new_file_name = f'{uuid}.{file_extension}'
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_extension = os.path.splitext(file_name)[1].lstrip('.')
new_file_name = f'{uuid}.{file_extension}'

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.

Открываем файл в двоичном режиме

Comment on lines +124 to +125
async with _S3Connector(S3Data) as s3:
await s3.upload_fileobj(fileobj=file_stream, key=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.

Загружаем файл на S3

Comment on lines +127 to +139
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()
Copy link
Owner Author

Choose a reason for hiding this comment

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

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

except Exception as e:
await session.rollback()

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.

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

Comment on lines +98 to +104
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
)
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

@reques6e reques6e added Улучшение Улучшение В работе В работе labels Feb 6, 2025
@reques6e reques6e changed the title Attachment Attachment (S3) Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
В работе В работе Улучшение Улучшение
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant