Skip to content

Commit e74e709

Browse files
committed
URL presigning starting to work
1 parent 31db2e0 commit e74e709

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

backend/app/api/deps.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ def get_current_active_superuser(current_user: CurrentUser) -> User:
5757
)
5858
return current_user
5959

60-
# dependency for AWS SDK boto3 session
61-
aws_session = boto3.Session(
60+
61+
# shared AWS SDK boto3 session
62+
s3_client = boto3.Session(
6263
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
6364
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
6465
region_name=settings.AWS_REGION,
65-
)
66-
AwsDep = Annotated[boto3.Session, Depends(lambda: aws_session)]
66+
).client('s3')

backend/app/api/routes/attachments.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import uuid
1+
import logging
22
from typing import Any
3+
import uuid
34

45
import boto3
56
from fastapi import APIRouter, HTTPException
67
from fastapi.responses import RedirectResponse
78
from sqlmodel import func, select
89

9-
from app.api.deps import AwsDep, CurrentUser, SessionDep
10-
from app.core.config import Settings
10+
from app.api.deps import CurrentUser, SessionDep, s3_client
11+
from app.core.config import settings
1112
from app.models.attachments import (
1213
Attachment,
1314
AttachmentCreate,
@@ -18,6 +19,8 @@
1819
)
1920
from app.models import Message
2021

22+
logger = logging.getLogger(__name__)
23+
2124
router = APIRouter(prefix="/attachments", tags=["attachments"])
2225

2326

@@ -36,7 +39,7 @@ def read_attachments(
3639
return AttachmentsPublic(data=attachments, count=count)
3740

3841
@router.get("/{id}/content")
39-
def read_attachment_content(session: SessionDep, aws_client: AwsDep, current_user: CurrentUser, id: uuid.UUID) -> Any:
42+
def read_attachment_content(session: SessionDep, current_user: CurrentUser, id: uuid.UUID) -> Any:
4043
"""
4144
Get attachment content by ID.
4245
"""
@@ -45,16 +48,17 @@ def read_attachment_content(session: SessionDep, aws_client: AwsDep, current_use
4548
raise HTTPException(status_code=404, detail="Attachment not found")
4649

4750
try:
48-
presigned_url = aws_client.generate_presigned_url(
51+
presigned_url = s3_client.generate_presigned_url(
4952
"get_object",
5053
Params={
51-
"Bucket": Settings.AWS_S3_ATTACHMENTS_BUCKET,
54+
"Bucket": settings.AWS_S3_ATTACHMENTS_BUCKET,
5255
"Key": attachment.storage_path,
5356
"ContentDisposition": f"attachment; filename={attachment.file_name}",
5457
},
5558
ExpiresIn=3600,
5659
)
5760
except Exception as e:
61+
logger.exception(e)
5862
raise HTTPException(status_code=500, detail="Could not generate presigned URL")
5963

6064
return RedirectResponse(status_code=302, url=presigned_url)
@@ -73,7 +77,7 @@ def read_attachment(session: SessionDep, current_user: CurrentUser, id: uuid.UUI
7377

7478
@router.post("/", response_model=AttachmentCreatePublic)
7579
def create_attachment(
76-
*, session: SessionDep, aws_client: AwsDep, current_user: CurrentUser, attachment_in: AttachmentCreate
80+
*, session: SessionDep, current_user: CurrentUser, attachment_in: AttachmentCreate
7781
) -> Any:
7882
"""
7983
Create a new attachment.
@@ -84,19 +88,21 @@ def create_attachment(
8488
session.refresh(attachment)
8589

8690
try:
87-
presigned_upload_url = aws_client.generate_presigned_url(
91+
presigned_upload_url = s3_client.generate_presigned_url(
8892
"put_object",
8993
Params={
90-
"Bucket": Settings.AWS_S3_ATTACHMENTS_BUCKET,
94+
"Bucket": settings.AWS_S3_ATTACHMENTS_BUCKET,
9195
"Key": attachment.storage_path,
9296
"ContentDisposition": f"attachment; filename={attachment.file_name}",
9397
},
9498
ExpiresIn=3600,
9599
)
96100
except Exception as e:
101+
logger.exception(e)
97102
raise HTTPException(status_code=500, detail="Could not generate presigned URL")
98103

99-
resmodel = AttachmentCreatePublic.model_validate(attachment)
100-
resmodel.upload_url = presigned_upload_url
104+
resmodel = AttachmentCreatePublic.model_validate(
105+
attachment, update={"upload_url": presigned_upload_url}
106+
)
101107

102108
return resmodel

backend/app/tests/api/routes/test_attachments.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_create_attachment(
3737
content = response.json()
3838
assert content["file_name"] == data["file_name"]
3939
assert content["mime_type"] == data["mime_type"]
40-
assert content["upload_url"] is not None
40+
assert content["upload_url"].startswith("https://")
4141
assert "id" in content
4242

4343
def test_read_attachment_details(

0 commit comments

Comments
 (0)