Skip to content

Commit 58a87ec

Browse files
chore: get public url
1 parent 675a2a5 commit 58a87ec

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

common/apps/upload_file/service.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ def _get_s3_client():
2626

2727

2828
def put_presigned_url(
29-
bucket_name, file_name, content_type, org_slug, visibility="private", expiration=3600
29+
bucket_name,
30+
file_name,
31+
content_type,
32+
org_slug,
33+
visibility="private",
34+
expiration=3600,
3035
):
3136
"""
3237
Generate a presigned URL for PUT upload to S3.
@@ -71,6 +76,23 @@ def put_presigned_url(
7176
return None
7277

7378

79+
def get_public_url(bucket_name, file_path):
80+
"""
81+
Build a permanent public URL for a public S3 object.
82+
83+
Args:
84+
bucket_name: S3 bucket name.
85+
file_path: Full S3 key starting with 'public/'.
86+
87+
Returns:
88+
Permanent public URL string.
89+
"""
90+
aws_config = getattr(settings, "AWS_S3", {})
91+
region = aws_config.get("AWS_REGION", "us-east-1")
92+
key = normalize_s3_key(file_path)
93+
return f"https://{bucket_name}.s3.{region}.amazonaws.com/{key}"
94+
95+
7496
def get_presigned_url(bucket_name, file_path, expiration=3600):
7597
"""
7698
Generate a presigned GET URL for an S3 object.

common/apps/upload_file/views.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from django.conf import settings
2+
from drf_yasg.utils import swagger_auto_schema
23
from rest_framework import status
34
from rest_framework.permissions import AllowAny
45
from rest_framework.response import Response
56
from rest_framework.views import APIView
6-
from drf_yasg.utils import swagger_auto_schema
77

88
from common.apps.upload_file.serializers import PresignedUploadSerializer
9-
from common.apps.upload_file.service import get_presigned_url, put_presigned_url
9+
from common.apps.upload_file.service import (
10+
get_presigned_url,
11+
get_public_url,
12+
put_presigned_url,
13+
)
1014

1115

1216
def _resolve_org_slug(request):
@@ -37,7 +41,10 @@ class PutPresignedURL(APIView):
3741

3842
@swagger_auto_schema(
3943
request_body=PresignedUploadSerializer,
40-
responses={200: "Presigned URL generated successfully", 500: "Internal server error"},
44+
responses={
45+
200: "Presigned URL generated successfully",
46+
500: "Internal server error",
47+
},
4148
)
4249
def post(self, request):
4350
serializer = PresignedUploadSerializer(data=request.data)
@@ -79,14 +86,19 @@ def get(self, request, file_path):
7986
status=status.HTTP_403_FORBIDDEN,
8087
)
8188

82-
if file_path.startswith("private/") and not request.user.is_authenticated:
89+
aws_s3_config = getattr(settings, "AWS_S3", {})
90+
bucket_name = aws_s3_config.get("AWS_STORAGE_BUCKET_NAME")
91+
92+
if file_path.startswith("public/"):
93+
url = get_public_url(bucket_name, file_path)
94+
return Response({"url": url}, status=status.HTTP_200_OK)
95+
96+
if not request.user.is_authenticated:
8397
return Response(
8498
{"error": "Authentication required for private files."},
8599
status=status.HTTP_401_UNAUTHORIZED,
86100
)
87101

88-
aws_s3_config = getattr(settings, "AWS_S3", {})
89-
bucket_name = aws_s3_config.get("AWS_STORAGE_BUCKET_NAME")
90102
url = get_presigned_url(bucket_name, file_path)
91103

92104
if url is not None:

0 commit comments

Comments
 (0)