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,3 @@
|
||||
GARAGE_ENDPOINT_URL=
|
||||
GARAGE_ACCESS_KEY=
|
||||
GARAGE_SECRET_KEY=
|
||||
@@ -11,3 +11,4 @@ app = FastAPI()
|
||||
app.include_router(projects.router, prefix="/api")
|
||||
app.include_router(experiences.router, prefix="/api")
|
||||
app.include_router(skills.router, prefix="/api")
|
||||
app.include_router(media.router, prefix="/api")
|
||||
@@ -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")
|
||||
@@ -0,0 +1,11 @@
|
||||
import os
|
||||
import boto3
|
||||
|
||||
s3_client = boto3.client(
|
||||
"s3",
|
||||
endpoint_url=os.getenv("GARAGE_ENDPOINT_URL", "http://127.0.0.1:3900"),
|
||||
aws_access_key_id=os.getenv("GARAGE_ACCESS_KEY"),
|
||||
aws_secret_access_key=os.getenv("GARAGE_SECRET_KEY"),
|
||||
)
|
||||
|
||||
BUCKET_NAME = os.getenv("GARAGE_BUCKET_NAME", "nom-de-ton-bucket")
|
||||
@@ -1,3 +1,22 @@
|
||||
fastapi
|
||||
uvicorn[standard]
|
||||
sqlalchemy
|
||||
annotated-doc==0.0.5
|
||||
annotated-types==0.8.0
|
||||
anyio==4.15.1
|
||||
boto3==1.43.108
|
||||
botocore==1.43.108
|
||||
click==8.5.0
|
||||
fastapi==0.142.2
|
||||
h11==0.16.0
|
||||
idna==3.20
|
||||
jmespath==1.1.0
|
||||
opentelemetry-api==1.45.0
|
||||
pydantic==2.13.5
|
||||
pydantic_core==2.46.5
|
||||
python-dateutil==2.9.0.post0
|
||||
s3transfer==0.19.2
|
||||
six==1.17.0
|
||||
SQLAlchemy==2.1.3
|
||||
starlette==1.7.0
|
||||
typing-inspection==0.4.4
|
||||
typing_extensions==4.16.0
|
||||
urllib3==2.8.0
|
||||
uvicorn==0.54.0
|
||||
Reference in new issue
Block a user