implement S3 garage on the backend
This commit is contained in:
1 parent
65e328fcc5
commit
6f66254577
6 files changed
+63
-6
No files matched your search
@@ -0,0 +1,23 @@
|
||||
from botocore.exceptions import ClientError
|
||||
from fastapi import APIRoute, APIRouter, HTTPException
|
||||
from fastapi.responses import StreamingResponse
|
||||
|
||||
from app.s3 import BUCKET_NAME, s3_client
|
||||
|
||||
router = APIRouter(prefix="/media", tags=["media"])
|
||||
|
||||
|
||||
@router.get("/{file_path:path}")
|
||||
async def get_media(file_path: str):
|
||||
try:
|
||||
s3_object = s3_client.get_object(Bucket=BUCKET_NAME, Key=file_path)
|
||||
|
||||
return StreamingResponse(
|
||||
content=s3_object["Body"].iter_chunks(),
|
||||
media_type=s3_object.get("ContentType", "application/octet-stream"),
|
||||
headers={"Cache-Control": "public, max-age=86400"},
|
||||
)
|
||||
except ClientError as e:
|
||||
if e.response["Error"]["Code"] in ["NoSuchKey", "404"]:
|
||||
raise HTTPException(status_code=404, detail="Fichier introuvable")
|
||||
raise HTTPException(status_code=500, detail="Erreur serveur S3")
|
||||
Reference in new issue
Block a user