Last active
June 12, 2018 11:48
-
-
Save mocyuto/97e360019c014ccf0584e1abb036a2f0 to your computer and use it in GitHub Desktop.
SNSからS3のイベントを受け取る
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"log" | |
"github.com/aws/aws-lambda-go/events" | |
"github.com/aws/aws-lambda-go/lambda" | |
) | |
type repositories struct { | |
repo infra.Repository | |
} | |
func (repo *repositories) handleRequest(event *events.SNSEvent) { | |
for _, r := range event.Records { | |
var s3event events.S3Event | |
if err := json.Unmarshal([]byte(r.SNS.Message), &s3event); err != nil { | |
log.Printf("failed to parse S3 event. err: %v", err) | |
} | |
repo.handleS3Event(&s3event) | |
} | |
} | |
func (repo *repositories) handleS3Event(s3event *events.S3Event) { | |
for _, r := range s3event.Records { | |
filePath := r.S3.Object.Key | |
repo.Insert(filePath) | |
} | |
} | |
func main() { | |
// 初期のenvの読み込み | |
s, err := getSpec() | |
if err != nil { | |
log.Fatalf("[ERROR] failed to load env: %s", err) | |
} | |
// DBのConnection生成 | |
db, err := openDB(s) | |
if err != nil { | |
log.Fatalf("[ERROR] Failed to connect database: %s", err) | |
} | |
repo := infra.NewRepository(db) | |
reops := repositories{repo: repo} | |
lambda.Start(repos.handleRequest) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment