LoginSignup
5
2

More than 5 years have passed since last update.

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

Last updated at Posted at 2018-12-24

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)
}
5
2
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
5
2