s3client-fix
All checks were successful
Deploy on Push / deploy (push) Successful in 31s

This commit is contained in:
Untone 2024-05-06 14:12:51 +03:00
parent c005c86303
commit d554d0ef14

20
main.py
View File

@ -1,4 +1,3 @@
import logging
import os import os
import tempfile import tempfile
import uuid import uuid
@ -8,6 +7,7 @@ from starlette.responses import JSONResponse
from starlette.routing import Route from starlette.routing import Route
from starlette.requests import Request from starlette.requests import Request
from auth import login_required from auth import login_required
from logger import root_logger as logger
from settings import ( from settings import (
PORT, PORT,
@ -18,10 +18,13 @@ from settings import (
STORJ_END_POINT, STORJ_END_POINT,
) )
# Logging configuration async def create_s3_client():
logging.basicConfig( return aioboto3.Session().create_client(
level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s" "s3",
) aws_access_key_id=STORJ_ACCESS_KEY,
aws_secret_access_key=STORJ_SECRET_KEY,
endpoint_url=STORJ_END_POINT,
)
@login_required @login_required
@ -38,12 +41,7 @@ async def upload_handler(request: Request):
key = str(uuid.uuid4()) + file_extension key = str(uuid.uuid4()) + file_extension
logging.debug(f"Generated file key: {key}") logging.debug(f"Generated file key: {key}")
async with aioboto3.client( async with create_s3_client() as s3:
"s3",
aws_access_key_id=STORJ_ACCESS_KEY,
aws_secret_access_key=STORJ_SECRET_KEY,
endpoint_url=STORJ_END_POINT,
) as s3:
with tempfile.NamedTemporaryFile() as tmp_file: with tempfile.NamedTemporaryFile() as tmp_file:
while True: while True:
chunk = await file.read(8192) chunk = await file.read(8192)