-
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
Conversation
Структура базы данных: 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 |
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.
В новой версии есть баг
'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') |
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.
Изменил сигнатуры на s3v4
uuid = uuid_generate.uuid4() | ||
file_extension = os.path.splitext(file_name)[1].lstrip('.') | ||
new_file_name = f'{uuid}.{file_extension}' |
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.
Необходимые данные.
file_extension = os.path.splitext(file_name)[1].lstrip('.') | ||
new_file_name = f'{uuid}.{file_extension}' | ||
|
||
file_stream = io.BytesIO(file_content) |
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.
Открываем файл в двоичном режиме
async with _S3Connector(S3Data) as s3: | ||
await s3.upload_fileobj(fileobj=file_stream, key=new_file_name) |
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
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() |
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.
Сохраняем данные в бд
except Exception as e: | ||
await session.rollback() | ||
|
||
return new_file_name |
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.
Тестовый ответ
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 | ||
) |
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
Нужно сделать методы Attachment для API.
Схема работы загрузки вложений:
Ответ от сервера: