先日、AWS Lambda で CloudWatch Event からトリガーした場合の event サンプルというメモを書きました。 Lambda のテスト用イベントとして予め用意されている Amazon S3 Put
の内容を自分用にメモしておきます。
Amazon S3 Put テストイベント
{ "Records": [ { "eventVersion": "2.0", "eventSource": "aws:s3", "awsRegion": "ap-northeast-1", "eventTime": "1970-01-01T00:00:00.000Z", "eventName": "ObjectCreated:Put", "userIdentity": { "principalId": "EXAMPLE" }, "requestParameters": { "sourceIPAddress": "127.0.0.1" }, "responseElements": { "x-amz-request-id": "EXAMPLE123456789", "x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH" }, "s3": { "s3SchemaVersion": "1.0", "configurationId": "testConfigRule", "bucket": { "name": "example-bucket", "ownerIdentity": { "principalId": "EXAMPLE" }, "arn": "arn:aws:s3:::example-bucket" }, "object": { "key": "test/key", "size": 1024, "eTag": "0123456789abcdef0123456789abcdef", "sequencer": "0A1B2C3D4E5F678901" } } } ] }
Bucket Name と Key を取得するサンプル
上記のテストイベントから Bucket Name と Key を取得するサンプルスクリプトは以下の通りです。 テストイベントは event.json
として保存してある前提です。
import json with open('event.json') as f: data = json.load(f) bucket = data['Records'][0]['s3']['bucket']['name'] key = data['Records'][0]['s3']['object']['key'] print(f'{bucket}/{key}')
実行例は以下の通りです。
$ python example.py
example-bucket/test/key