Skip to content

Commit

Permalink
Merge pull request #49 from idkbrowby/vivek/inactive-bucket
Browse files Browse the repository at this point in the history
Handle error when object storage is inactive or down.
  • Loading branch information
anand2312 authored Oct 18, 2023
2 parents 2aa4f09 + e2f1016 commit 520a578
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/backend/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import uuid
from typing import Literal

import httpx
from dotenv import load_dotenv
from fastapi import HTTPException, UploadFile
from storage3 import AsyncStorageClient, create_client
Expand Down Expand Up @@ -36,8 +37,9 @@ def _validate_file(file: UploadFile) -> Literal[True]:

async def _upload_to_storage(files: list[UploadFile], destination: str) -> list[str]:
for file in files:
assert file.filename is not None
_validate_file(file)
if file.filename is None:
raise HTTPException(status_code=400, detail="No filename provided.")
_validate_file(file)

urls = []
filenames = set()
Expand All @@ -60,6 +62,11 @@ async def _upload_to_storage(files: list[UploadFile], destination: str) -> list[
raise HTTPException(
status_code=error_details["statusCode"], detail=error_details["message"]
)
except httpx.ConnectError:
raise HTTPException(
status_code=503,
detail="Post could not be uploaded as object storage is not reachable.",
)
else:
_, path = res.json()["Key"].split("/", 1)
urls.append(await storage_client.from_(BUCKET_NAME).get_public_url(path))
Expand Down

0 comments on commit 520a578

Please sign in to comment.