For studying Lambda. Extract the gz file uploaded to the s3 bucket and output it to CloudWatch Logs.
analyze_s3logs
from __future__ import print_function
import json
import urllib
import boto3
import gzip
print('Loading function')
s3 = boto3.client('s3')
def lambda_handler(event, context):
print("[LambdaLog] Received event: " + json.dumps(event, indent=2))
# Get the object from the event and show its content type
bucket = event['Records'][0]['s3']['bucket']['name']
key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']).decode('utf8')
try:
s3.download_file(bucket, key, '/tmp/file.dat')
if ('.gz' in key):
f = gzip.open('/tmp/file.dat', 'rb')
else:
f = open('/tmp/file.dat', 'r')
content = f.read()
f.close
print(content)
return 0
except Exception as e:
print(e)
print('[LambdaLog] Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
raise e
Recommended Posts