-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (61 loc) · 1.41 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"os"
)
func main() {
if os.Getenv("LOCAL_INVOKE_GZ_PATH") == "" {
lambda.Start(handler)
} else {
if err := devHandler(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}
}
func devHandler() error {
fmt.Println("start download from s3")
processor, err := NewProcessor()
if err != nil {
return err
}
f, err := os.Open(os.Getenv("LOCAL_INVOKE_GZ_PATH"))
if err != nil {
return err
}
defer f.Close()
err = processor.ProcessLogfile(f, f.Name())
if err != nil {
return err
}
return nil
}
func handler(s3Event events.S3Event) error {
fmt.Println("start handler")
processor, err := NewProcessor()
if err != nil {
return err
}
sess := session.Must(session.NewSession(aws.NewConfig()))
for _, record := range s3Event.Records {
fmt.Printf("%+v\n", record)
svc := s3.New(sess)
obj, err := svc.GetObject(&s3.GetObjectInput{Bucket: aws.String(record.S3.Bucket.Name), Key: aws.String(record.S3.Object.Key)})
if err != nil {
return err
}
fmt.Println("finish download from s3")
err = processor.ProcessLogfile(obj.Body, record.S3.Object.Key)
if err != nil {
return err
}
fmt.Println("finish processLogFile")
}
fmt.Println("finish handler")
return nil
}