2
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

posted at

updated at

Go言語でS3のイベントをHookしてLambdaを実行するときの書き方

S3のイベントをHookしてLambdaを実行するサンプルはいくつかあったが、Go言語の情報がほとんどなかったのでテンプレだけ書いておく。
eventは自作せず、aws-lambda-goが提供しているevents.S3Eventを使う
events.S3Eventの中にはevent.Recordsがあり、その中にイベントを発生させたS3のBucket,Keyが入っている

package main

import (
    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func s3lambda(ctx context.Context, event events.S3Event) (interface{}, error) {
    for _, record := range event.Records {
        // recordの中にイベント発生させたS3のBucket名やKeyが入っている     
        bucket := record.S3.Bucket.Name
        key := record.S3.Object.Key
    }
    resp := &struct {
        StatusCode uint `json:"statusCode"`
    }{StatusCode: 200}
    return resp, nil
}

func main() {
    lambda.Start(s3lambda)
}
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
2
Help us understand the problem. What are the problem?