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
|
package main
import (
"bytes"
"context"
"fmt"
"image"
"image/jpeg"
"io"
"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/disintegration/imaging"
)
func main() {
lambda.Start(handler)
}
func handler(ctx context.Context, s3Event events.S3Event) error {
sess, err := session.NewSession(&aws.Config{
// Tokyo region is ap-northeast-1
Region: aws.String("ap-northeast-1")},
)
if err != nil {
// check error, lambda logs also show up in cloudwatch
fmt.Println("message: error = ", err.Error())
return err
}
svc := s3.New(sess)
fmt.Println("message: get session")
// only deal with one file at a time
object := s3Event.Records[0].S3.Object
// key is the file name with all prefix
key := object.Key
// bucket is the bucket name
bucket := "{Your Bucket Name}"
// check the file key
fmt.Println("message: key = ", key)
resp, err := svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
fmt.Println("message: error = ", err.Error())
return err
}
defer resp.Body.Close()
content, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("message: error = ", err.Error())
return err
}
img, _, err := image.Decode(bytes.NewReader(content))
if err != nil {
fmt.Println("message: error = ", err.Error())
return err
}
resized := imaging.Resize(img, 3000, 3000, imaging.Lanczos)
buf := new(bytes.Buffer)
err = jpeg.Encode(buf, resized, nil)
if err != nil {
fmt.Println("message: error = ", err.Error())
return err
}
// get the file name with prefix
keyPath := strings.Split(key, "/")
imageName := keyPath[len(keyPath)-1]
// put the new resized image to the another folder, avoid infinite loop
newKey := "/podcast/images/" + imageName
_, err = svc.PutObject(&s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(newKey),
ContentType: aws.String("image/jpeg"),
Body: bytes.NewReader(buf.Bytes()),
})
if err != nil {
fmt.Println("message: error = ", err.Error())
return err
}
fmt.Println("message: success")
return nil
}
|