Skip to content

Commit

Permalink
feat: add s3 function to get path org_id/proj_id
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Jan 8, 2024
1 parent 11c08f5 commit 92fba46
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/backend/app/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from loguru import logger as log
from minio import Minio
from minio.commonconfig import CopySource
from sqlalchemy import text
from sqlalchemy.orm import Session

from app.config import settings

Expand Down Expand Up @@ -113,7 +115,7 @@ def get_obj_from_bucket(bucket_name: str, s3_path: str) -> BytesIO:
response = client.get_object(bucket_name, s3_path)
return BytesIO(response.read())
except Exception as e:
raise ValueError(str(e))
raise ValueError(str(e)) from e
finally:
if response:
response.close()
Expand Down Expand Up @@ -159,6 +161,16 @@ def copy_obj_bucket_to_bucket(
return True


async def get_bucket_path(db: Session, project_id: int) -> str:
"""For a given project id, return the S3 bucket path."""
log.debug(f"Getting org id for project: {project_id}")
query = text(f"SELECT organisation_id FROM projects WHERE id = {project_id};")
result = db.execute(query)
org_id = result.fetchone()[0]
log.debug(f"Extracted org id: {org_id}")
return f"/{org_id}/{project_id}"


def create_bucket_if_not_exists(client: Minio, bucket_name: str, is_public: bool):
"""Checks if a bucket exits, else creates it."""
if not client.bucket_exists(bucket_name):
Expand Down

0 comments on commit 92fba46

Please sign in to comment.