LoginSignup
6

More than 5 years have passed since last update.

s3に圧縮ファイルがアップされた際に、解凍して要素をCloudWatchLogsに出力するLambda Function(python版)

Last updated at Posted at 2015-11-13

Lambdaのお勉強がてらに。
s3バケットにアップロードされたgzファイルを展開して、CloudWatchLogsに出力する。

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6