23 lines
877 B
Python
23 lines
877 B
Python
from botocore.exceptions import ClientError
|
|
from fastapi import 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") |