-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the img uploader API (#266)
* add new env params: * AWS_SECRET_NAME * AWS_REGION_NAME * S3_BUCKET_NAME * STATIC_URL
- Loading branch information
Showing
18 changed files
with
234 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from petercat_utils.utils.env import get_env_variable | ||
|
||
SUCCESS_CODE = "UPLOAD_SUCCESS" | ||
ERROR_CODES = {"credentials_error": "CREDENTIALS_ERROR", "upload_error": "UPLOAD_ERROR"} | ||
S3_BUCKET_NAME = get_env_variable("S3_BUCKET_NAME") | ||
STATIC_URL = get_env_variable("STATIC_URL") | ||
AWS_REGION_NAME = get_env_variable("AWS_REGION_NAME") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from .constants import AWS_REGION_NAME | ||
import boto3 | ||
|
||
|
||
def get_s3_client(): | ||
session = boto3.session.Session() | ||
client = session.client(service_name="s3", region_name=AWS_REGION_NAME) | ||
return client |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from fastapi import HTTPException | ||
|
||
|
||
class UploadError(HTTPException): | ||
def __init__(self, detail: str): | ||
super().__init__(status_code=500, detail=detail) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from fastapi import APIRouter, Depends, File, UploadFile, Form | ||
from .schemas import ImageMetaData | ||
from .dependencies import get_s3_client | ||
from .service import upload_image_to_s3 | ||
|
||
router = APIRouter( | ||
prefix="/api/aws", | ||
tags=["aws"], | ||
responses={404: {"description": "Not found"}}, | ||
) | ||
|
||
|
||
@router.post("/upload") | ||
async def upload_image( | ||
file: UploadFile = File(...), | ||
title: str = Form(None), | ||
description: str = Form(None), | ||
s3_client=Depends(get_s3_client), | ||
): | ||
metadata = ImageMetaData(title=title, description=description) | ||
result = upload_image_to_s3(file, metadata, s3_client) | ||
return {"status": "success", "data": result} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from pydantic import BaseModel | ||
from typing import Optional | ||
|
||
|
||
class ImageMetaData(BaseModel): | ||
title: Optional[str] = None | ||
description: Optional[str] = None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from .schemas import ImageMetaData | ||
from .constants import S3_BUCKET_NAME, STATIC_URL | ||
from .exceptions import UploadError | ||
|
||
|
||
def upload_image_to_s3(file, metadata: ImageMetaData, s3_client): | ||
try: | ||
file_content = file.file.read() | ||
|
||
s3_key = f"{file.filename}" | ||
|
||
custom_metadata = { | ||
"title": metadata.title if metadata.title else "", | ||
"description": metadata.description if metadata.description else "", | ||
} | ||
|
||
s3_client.put_object( | ||
Bucket=S3_BUCKET_NAME, | ||
Key=s3_key, | ||
Body=file_content, | ||
ContentType=file.content_type, | ||
Metadata=custom_metadata, | ||
) | ||
# you need to redirect your static domain to your s3 bucket domain | ||
s3_url = f"{STATIC_URL}/{s3_key}" | ||
return {"message": "File uploaded successfully", "url": s3_url} | ||
except Exception as e: | ||
raise UploadError(detail=str(e)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.