in source/lambda_function.py [0:0]
def download_image_from_s3(presigned_s3_url):
REKOGNITION_MAX_FILE_SIZE = 5242880
response = http.request('GET', presigned_s3_url)
if response is None:
raise ValueError('response is empty')
if response.headers is None:
raise ValueError('response has no headers')
if response.status >= 300:
s3_error_code, s3_error_message = parse_s3_error_response(response.data.decode('utf-8'))
raise S3GetException(
s3_http_code = response.status,
s3_error_code = s3_error_code,
s3_error_message = "S3 Error: " + s3_error_message
)
extensions = ['/jpg', '/jpeg', '/png']
if not any(response.headers['Content-Type'].lower().endswith(ext) for ext in extensions):
raise UnsupportedFormatException()
file_format = response.headers['Content-Type'].split('/')[-1]
if int(response.headers['Content-Length']) > REKOGNITION_MAX_FILE_SIZE:
raise ExceedingFileSizeException()
# Translate the headers returned from the presigned s3 url request to a format used by
# S3 WriteGetObjectResponse.
headers = translate_response_headers_to_writegetobjectresponse(response.headers)
return response.data, headers, file_format