Skip to content

Commit

Permalink
fix: presigned url expiry time
Browse files Browse the repository at this point in the history
  • Loading branch information
nrjadkry authored Sep 24, 2024
1 parent 1b7520a commit 3358ec2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/backend/app/projects/project_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,12 @@ async def get_project_info_from_s3(project_id: uuid.UUID, task_id: uuid.UUID):
# Generate a presigned URL for the assets ZIP file
try:
# Check if the object exists
assets_path = f"processed/{project_id}/{task_id}/assets.zip"
assets_path = f"projects/{project_id}/{task_id}/assets.zip"
get_object_metadata(settings.S3_BUCKET_NAME, assets_path)

# If it exists, generate the presigned URL
presigned_url = get_presigned_url(
settings.S3_BUCKET_NAME, assets_path, expires=3600
settings.S3_BUCKET_NAME, assets_path, expires=2
)
except S3Error as e:
if e.code == "NoSuchKey":
Expand Down
9 changes: 5 additions & 4 deletions src/backend/app/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from minio import Minio
from io import BytesIO
from typing import Any
from datetime import timedelta


def s3_client():
Expand Down Expand Up @@ -182,20 +183,20 @@ def list_objects_from_bucket(bucket_name: str, prefix: str):
return objects


def get_presigned_url(bucket_name: str, object_name: str, expires: int = 3600):
def get_presigned_url(bucket_name: str, object_name: str, expires: int = 2):
"""Generate a presigned URL for an object in an S3 bucket.
Args:
bucket_name (str): The name of the S3 bucket.
object_name (str): The name of the object in the bucket.
expires (int, optional): The time in seconds until the URL expires.
Defaults to 3600.
expires (int, optional): The time in hours until the URL expires.
Defaults to 2 hour.
Returns:
str: The presigned URL to access the object.
"""
client = s3_client()
return client.presigned_get_object(bucket_name, object_name, expires=expires)
return client.presigned_get_object(bucket_name, object_name, expires=timedelta(hours=expires))


def get_object_metadata(bucket_name: str, object_name: str):
Expand Down

0 comments on commit 3358ec2

Please sign in to comment.