-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
143 lines (118 loc) · 3.26 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
141
142
143
package main
import (
"context"
"fmt"
"image"
"image/color"
"os"
"path/filepath"
"strings"
"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"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/disintegration/imaging"
log "github.com/sirupsen/logrus"
)
// temp location to store image and thumbnail
const tmp = "/tmp/"
// S3 Session to use
var sess = session.Must(session.NewSession())
// Create an uploader with session and default option
var uploader = s3manager.NewUploader(sess)
// Create a downloader with session and default option
var downloader = s3manager.NewDownloader(sess)
func handle(ctx context.Context, req events.S3Event) (string, error) {
log.SetOutput(os.Stdout)
log.Infof("%v", req)
for _, r := range req.Records {
if key := r.S3.Object.Key; isImage(key) {
// generate thumbnail
bucket := r.S3.Bucket.Name
genThumb(bucket, key)
}
}
return fmt.Sprintf("%d records processed", len(req.Records)), nil
}
func genThumb(bucket, key string) {
local := tmp + bucket + "/" + key
// ensure path is available
dir := filepath.Dir(local)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
log.WithError(err).WithField("path", dir).Error("failed to create tmp directory")
}
// create a file locally for original image in S3
f, err := os.Create(local)
if err != nil {
log.WithError(err).WithField("filename", local).Error("failed to create file")
return
}
n, err := downloader.Download(f, &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
log.WithError(err).WithFields(log.Fields{
"bucket": bucket,
"key": key,
"filename": local,
}).Error("failed to download file")
return
}
log.WithFields(log.Fields{
"filename": local,
"bytes": n,
}).Info("file downloaded")
img, err := imaging.Open(local)
if err != nil {
panic(err)
}
thumb := imaging.Thumbnail(img, 100, 100, imaging.CatmullRom)
// create a new blank image
dst := imaging.New(100, 100, color.NRGBA{0, 0, 0, 0})
// paste thumbnails into the new image
dst = imaging.Paste(dst, thumb, image.Pt(0, 0))
// save the combined image to file
thumbName := key[:len(key)-4] + "_thumb" + key[len(key)-4:]
thumbLocal := "/tmp/" + thumbName
err = imaging.Save(dst, thumbLocal)
if err != nil {
log.WithError(err).WithField("thumbnail", thumbLocal).Error("failed to generate thumbnail")
return
}
// upload thumbnail to S3
up, err := os.Open(thumbLocal)
if err != nil {
log.WithError(err).WithField("thumbnail", thumbLocal).Error("failed to open file")
return
}
result, err := uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(thumbName),
Body: up,
})
if err != nil {
log.WithError(err).WithFields(log.Fields{
"bucket": bucket,
"thumbnail": thumbName,
}).Error("failed to upload file")
}
log.WithField("location", result.Location).Info("file uploaded")
}
func isImage(name string) bool {
if strings.HasSuffix(name, ".jpg") {
return true
}
if strings.HasSuffix(name, ".png") {
return true
}
if strings.HasSuffix(name, ".gif") {
return true
}
return false
}
func main() {
lambda.Start(handle)
}