Commit ·
3ea741a
1
Parent(s): 422db8f
feat: add upload file feature
Browse files- __pycache__/app.cpython-312.pyc +0 -0
- app.py +33 -0
__pycache__/app.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/app.cpython-312.pyc and b/__pycache__/app.cpython-312.pyc differ
|
|
|
app.py
CHANGED
|
@@ -12,11 +12,25 @@ from pydantic import BaseModel
|
|
| 12 |
from enum import Enum
|
| 13 |
from typing import Optional, Dict, List
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
load_dotenv()
|
| 16 |
|
| 17 |
app = FastAPI()
|
| 18 |
BASE_URL = os.getenv("BASE_URL")
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def get_db():
|
| 22 |
db = SessionLocal()
|
|
@@ -43,6 +57,25 @@ class InterviewUpdate(BaseModel):
|
|
| 43 |
expired_date: Optional[datetime] = None
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
@app.post("/generate_link")
|
| 47 |
def generate_link(payload: InterviewUpdate):
|
| 48 |
db: Session = next(get_db())
|
|
|
|
| 12 |
from enum import Enum
|
| 13 |
from typing import Optional, Dict, List
|
| 14 |
|
| 15 |
+
from fastapi import FastAPI, UploadFile, File
|
| 16 |
+
from google.cloud import storage
|
| 17 |
+
from google.oauth2 import service_account
|
| 18 |
+
|
| 19 |
load_dotenv()
|
| 20 |
|
| 21 |
app = FastAPI()
|
| 22 |
BASE_URL = os.getenv("BASE_URL")
|
| 23 |
|
| 24 |
+
# gcp_private_key = os.getenv("GCP_PRIVATE_KEY")
|
| 25 |
+
GCP_SERVICE_ACCOUNT = os.getenv("GCP_CREDENTIAL")
|
| 26 |
+
# GCP_SERVICE_ACCOUNT["private_key"] = gcp_private_key
|
| 27 |
+
|
| 28 |
+
GCP_BUCKET_NAME = os.getenv("GCP_BUCKET_NAME")
|
| 29 |
+
|
| 30 |
+
credentials = service_account.Credentials.from_service_account_info(GCP_SERVICE_ACCOUNT)
|
| 31 |
+
client = storage.Client(credentials=credentials)
|
| 32 |
+
bucket = client.bucket(GCP_BUCKET_NAME)
|
| 33 |
+
|
| 34 |
|
| 35 |
def get_db():
|
| 36 |
db = SessionLocal()
|
|
|
|
| 57 |
expired_date: Optional[datetime] = None
|
| 58 |
|
| 59 |
|
| 60 |
+
@app.post("/upload")
|
| 61 |
+
async def upload_file(file: UploadFile = File(...)):
|
| 62 |
+
blob = bucket.blob(file.filename)
|
| 63 |
+
|
| 64 |
+
blob.upload_from_file(
|
| 65 |
+
file.file,
|
| 66 |
+
content_type=file.content_type
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
return {
|
| 70 |
+
"code":201,
|
| 71 |
+
"message": "Request was successful.",
|
| 72 |
+
"data":{
|
| 73 |
+
"filename": file.filename,
|
| 74 |
+
"url": f"https://storage.googleapis.com/{bucket.name}/{file.filename}"
|
| 75 |
+
}
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
|
| 79 |
@app.post("/generate_link")
|
| 80 |
def generate_link(payload: InterviewUpdate):
|
| 81 |
db: Session = next(get_db())
|