ss3
This commit is contained in:
parent
c7d67c7a01
commit
8a62217eb6
|
|
@ -1,5 +1,6 @@
|
|||
from .chain import router as chain_router
|
||||
from .run_chain import router as run_chain_router
|
||||
from .s3 import router as s3_router
|
||||
|
||||
from fastapi import FastAPI, APIRouter
|
||||
|
||||
|
|
@ -9,5 +10,6 @@ def setup_controllers(application: FastAPI):
|
|||
|
||||
router.include_router(chain_router)
|
||||
router.include_router(run_chain_router)
|
||||
router.include_router(s3_router)
|
||||
|
||||
application.include_router(router)
|
||||
|
|
|
|||
28
chain_service/controllers/s3.py
Normal file
28
chain_service/controllers/s3.py
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
from chain_service.s3 import s3_client
|
||||
from chain_service.settings import Settings
|
||||
|
||||
from io import BytesIO
|
||||
from uuid import uuid4
|
||||
from loguru import logger
|
||||
from fastapi import APIRouter, HTTPException, UploadFile
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/s3")
|
||||
async def s3_controller(file: UploadFile):
|
||||
try:
|
||||
filename = f'{uuid4()}.{file.filename.split(".")[-1]}'
|
||||
|
||||
upload_file_bucket = Settings().s3_upload_file_bucket
|
||||
upload_file_key = f"files/{filename}"
|
||||
|
||||
s3_client.upload_fileobj(
|
||||
BytesIO(await file.read()), upload_file_bucket, upload_file_key
|
||||
)
|
||||
|
||||
return {"url": Settings().s3_public_url + upload_file_key}
|
||||
|
||||
except Exception:
|
||||
logger.exception("Error during s3 upload")
|
||||
return HTTPException(status_code=500, detail="Cannot upload")
|
||||
10
chain_service/s3.py
Normal file
10
chain_service/s3.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import boto3
|
||||
from chain_service.settings import Settings
|
||||
|
||||
settings = Settings()
|
||||
|
||||
s3_client = boto3.client(
|
||||
"s3",
|
||||
aws_access_key_id=settings.s3_access_key,
|
||||
aws_secret_access_key=settings.s3_secret_access_key,
|
||||
)
|
||||
Loading…
Reference in New Issue
Block a user