in source/lambda_function.py [0:0]
def lambda_handler(event, context):
try:
[request_route, request_token, presigned_s3_url] = extract_object_context(event)
# Download the image object from S3 via the presigned url.
# This way the object lambda does not need S3 read permissions.
# (Generally, Rekognition would also accept passing an S3 URL instead of the image bytes)
[image_bytes, headers, file_format] = download_image_from_s3(presigned_s3_url)
# Detect moderation content in the image with Rekognition
response = rekognition.detect_moderation_labels(
Image={
'Bytes': image_bytes
},
MinConfidence=50
)
#Blur the imaege if moderation labels are present
labels = response['ModerationLabels']
if len(labels):
image_bytes = blur_image(image_bytes, file_format)
# Update the headers to the new size in bytes of the blurred image (in case it changed)
headers['ContentLength']=len(image_bytes)
except Exception as e:
handle_exception(e, request_route, request_token)
else:
write_s3_get_object_response(200, request_route, request_token, image_bytes, headers)
# Return lambda gracefully
return {'statusCode': 200 }