-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
140 lines (115 loc) · 3.02 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"bytes"
"context"
"errors"
"fmt"
"image"
"image/jpeg"
_ "image/jpeg"
"image/png"
_ "image/png"
"io/ioutil"
"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/client"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/disintegration/imaging"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
DEFAULT_IMAGE_SIZE = 800
)
func createS3Client(awsSession client.ConfigProvider) *s3.S3 {
return s3.New(awsSession)
}
func getBytesFromS3(s3Svc *s3.S3, bucket string, key string) ([]byte, error) {
resp, err := s3Svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
return nil, err
}
return ioutil.ReadAll(resp.Body)
}
func createLogger() (*zap.Logger, error) {
logConfig := zap.Config{
OutputPaths: []string{"stdout"},
Level: zap.NewAtomicLevelAt(zapcore.InfoLevel),
Encoding: "json",
EncoderConfig: zapcore.EncoderConfig{
LevelKey: "level",
TimeKey: "time",
MessageKey: "msg",
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeLevel: zapcore.LowercaseLevelEncoder,
},
}
return logConfig.Build()
}
func getImg(imgBytes []byte) (image.Image, string, error) {
return image.Decode(bytes.NewReader(imgBytes))
}
func resizeImg(img image.Image) image.Image {
resizedImg := imaging.Resize(img, DEFAULT_IMAGE_SIZE, 0, imaging.Lanczos)
return resizedImg
}
func writeImg(s3Svc *s3.S3, bucket string, key string, img image.Image, format string) error {
buff := new(bytes.Buffer)
var err error
switch format {
case "png":
err = png.Encode(buff, img)
case "jpeg":
err = jpeg.Encode(buff, img, nil)
default:
err = errors.New("Unsuport format")
}
if err != nil {
fmt.Println("failed to create buffer", err)
}
_, err = s3Svc.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: bytes.NewReader(buff.Bytes()),
})
if err != nil {
return err
}
return nil
}
func Handler(ctx context.Context, req events.S3Event) (string, error) {
logger, err := createLogger()
if err != nil {
return "", err
}
defer logger.Sync() // nolint
logger.Info("convert started")
logger.Info("create session")
awsSession := session.Must(session.NewSession(&aws.Config{Region: aws.String("us-east-2")}))
s3Svc := createS3Client(awsSession)
imgBytes, err := getBytesFromS3(s3Svc, req.Records[0].S3.Bucket.Name, req.Records[0].S3.Object.Key)
logger.Info("read image")
img, format, err := getImg(imgBytes)
if err != nil {
logger.Warn("failed to read image", zap.Error(err))
return "", err
}
logger.Info("resize image")
resizedImg := resizeImg(img)
logger.Info("write image")
err = writeImg(s3Svc, "reserva-converted-img", req.Records[0].S3.Object.Key, resizedImg, format)
if err != nil {
logger.Warn("failed to write image", zap.Error(err))
return "", err
}
logger.Info("convert completed")
return "", nil
}
func main() {
lambda.Start(Handler)
}