diff --git a/api/db.py b/api/db.py index 722a5dc..ee21ba5 100644 --- a/api/db.py +++ b/api/db.py @@ -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: path = get_s3_image_path(self.base_filename, self.data_type, "10", "jpg") url = get_s3_file_url(path) package["jpg_url"] = url diff --git a/api/helpers.py b/api/helpers.py index cea43db..91574db 100644 --- a/api/helpers.py +++ b/api/helpers.py @@ -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'] @@ -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