From 1b2f2b1a7f12cef801922eb5561fa402a9954d32 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 4 Mar 2022 21:04:23 +0100 Subject: file_storage.s3: Return None on NoSuchKey errors This will cause the file handler to return 404 instead of 500. --- fbin/file_storage/s3.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'fbin') diff --git a/fbin/file_storage/s3.py b/fbin/file_storage/s3.py index b372dd7..25b204c 100644 --- a/fbin/file_storage/s3.py +++ b/fbin/file_storage/s3.py @@ -62,7 +62,12 @@ class Storage(BaseStorage): kwargs = {} if 'Range' in request.headers: kwargs['Range'] = request.headers['Range'] - data = obj.get(**kwargs) + try: + data = obj.get(**kwargs) + except botocore.exceptions.ClientError as e: + if e.response['Error']['Code'] == 'NoSuchKey': + return + raise rv = send_file(data['Body'], attachment_filename=f.filename) rv.headers['Content-Length'] = data['ContentLength'] rv.headers['Accept-Ranges'] = data['AcceptRanges'] -- cgit v1.2.3