Skip to content

Commit

Permalink
raise error for presigned URL of nonexistent file
Browse files Browse the repository at this point in the history
  • Loading branch information
kciurleo committed Feb 15, 2023
1 parent a4082ae commit e17cd6f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
5 changes: 2 additions & 3 deletions api/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,8 @@ def get_image_pkg(self):
package["jpg_url"] = ""
if self.jpg_medium_exists:
path = get_s3_image_path(self.base_filename, self.data_type, "20", "jpg")
try:
url = get_s3_file_url(path)
except:
url = get_s3_file_url(path)
if url is None:

This comment has been minimized.

Copy link
@timbeccue

timbeccue Mar 2, 2023

Collaborator

I think this may be a bug, as the s3 url can be created without its referenced object actually existing. The url can exist but you just get an error when using it.

path = get_s3_image_path(self.base_filename, self.data_type, "10", "jpg")
url = get_s3_file_url(path)
package["jpg_url"] = url
Expand Down
16 changes: 11 additions & 5 deletions api/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging

from botocore.client import Config
from botocore.exceptions import ClientError

BUCKET_NAME = os.environ['S3_BUCKET_NAME']
CONFIG_TABLE_NAME = os.environ['CONFIG_TABLE_NAME']
Expand Down Expand Up @@ -111,10 +112,15 @@ def get_s3_file_url(path, ttl=604800):
"""Generates a presigned URL for a file at AWS."""

s3 = boto3.client('s3', REGION, config=Config(signature_version='s3v4'))
url = s3.generate_presigned_url(
ClientMethod="get_object",
Params={"Bucket": BUCKET_NAME, "Key": path},
ExpiresIn=ttl
)
try:
url = s3.generate_presigned_url(
ClientMethod="get_object",
Params={"Bucket": BUCKET_NAME, "Key": path},
ExpiresIn=ttl
)
except ClientError as e:
logging.error(e)
return None

return url

0 comments on commit e17cd6f

Please sign in to comment.