1
1

More than 1 year has passed since last update.

AWS S3に格納したファイルをLambdaで処理しS3に再格納する

Posted at

AWS Lambdaの学習をしています。
S3に格納したファイルをLambdaで処理し、再度S3に格納するということをやってみたいと思い取り組んでみました。
今回はその時の学んだことを記載します。

予め実施しておくべきこと

  • LambdaのトリガーにS3を登録する。(今回はinput/配下の.csvを対象にした)
  • IAMのロールより作成したLambda関数にS3への許可を設定する。

学んだこと

  • boto3:AWSが提供しているPythonでAWSを操作可能とするライブラリの名称
  • lambda_handler:Lambdaの実行時に呼び出される関数

作成した関数

import boto3


s3 = boto3.client('s3')

def read_file(bucket_name, file_key):
    response = s3.get_object(Bucket=bucket_name, Key=file_key)
    print("response = ", response)
    return response[u'Body'].read().decode('utf-8')

def upload_file(bucket_name, file_key, bytes):
    out_s3 = boto3.resource('s3')
    s30bj = out_s3.Object(bucket_name, file_key)
    res = s30bj.put(Body = bytes)
    return res

def lambda_handler(event, context):
    print("*** Lambdaが呼ばれました ***")
    print("event = ", event)
    # バケット名を取得
    input_bucket = event['Records'][0]['s3']['bucket']['name']
    print("input_bucket = ", input_bucket)
    # inputのキーを取得
    input_key = event['Records'][0]['s3']['object']['key']
    print("input_key = ", input_key)
    # inputのファイル名を取得
    input_filename = (input_key.split('/',1)[-1])
    print("input_filename = ", input_filename)

    # S3からファイルを読み込み
    print("*** s3からファイルを読み込みします ***")
    text = read_file(input_bucket, input_key)
    print("text = ", text)

    # outputのキーを設定
    print("*** outputのキーを設定します ***")
    output_key = 'output/' + 'after_' + input_filename
    print("output_key = ", output_key)

    # ファイルをS3に出力
    print("*** ファイルをs3に出力します ***")
    upload_file(input_bucket, output_key, bytes(text, 'utf-8'))
    print("*** Lambdaを終了します ***")

実行した結果

S3のinput/配下に任意のcsvをアップロードしたところ、
output/配下に"after_"を付与した名称でアップロードされるようになった。

1
1
0

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
1
1