Created
June 17, 2019 22:27
-
-
Save kevinslin/82506a0ecdc65f5a4ac75d09ac44ea34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lambda_handler(event, context): | |
""" | |
Main event handler, calls thumbor with received event. | |
""" | |
try: | |
start_time = timeit.default_timer() | |
global log_level | |
log_level = str(os.environ.get('LOG_LEVEL')).upper() | |
if log_level not in [ | |
'DEBUG', 'INFO', | |
'WARNING', 'ERROR', | |
'CRITICAL' | |
]: | |
log_level = 'ERROR' | |
logging.getLogger().setLevel(log_level) | |
if event['requestContext']['httpMethod'] != 'GET' and\ | |
event['requestContext']['httpMethod'] != 'HEAD': | |
return response_formater(status_code=405) | |
result = call_thumbor(event) | |
if str(os.environ.get('SEND_ANONYMOUS_DATA')).upper() == 'YES': | |
send_metrics(event, result, start_time) | |
# save image to other bucket before exiting | |
if result['statusCode'] == 200: | |
import boto3 | |
# check event context for API method and use that | |
# to decide what bucket to write to | |
# TODO: something you would implement | |
bucket = derive_bucket_from_event(event) | |
prefix = os.path.basename(event['path']) | |
s3 = boto3.resource('s3') | |
s3.Object(bucket, prefix).put(Body=result['body']) | |
return result | |
except Exception as error: | |
logging.error('lambda_handler error: %s' % (error)) | |
logging.error('lambda_handler trace: %s' % traceback.format_exc()) | |
return response_formater(status_code='500', | |
cache_control='no-cache,no-store') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment