forked from h2non/imaginary
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsource_s3.go
174 lines (145 loc) · 4.21 KB
/
source_s3.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"bytes"
"fmt"
"gopkg.in/h2non/bimg.v1"
"gopkg.in/h2non/filetype.v1"
"net/http"
"os"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"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"
)
const ImageSourceTypeS3 ImageSourceType = "s3"
func init() {
RegisterSource(ImageSourceTypeS3, NewS3ImageSource)
}
func newS3Session(region string) (*session.Session, error) {
return session.NewSession(&aws.Config{
Region: ®ion,
Credentials: credentials.NewStaticCredentials(
os.Getenv("S3_KEY"),
os.Getenv("S3_KEY_SECRET"),
"",
),
})
}
type S3ImageSource struct {
Config *SourceConfig
}
func NewS3ImageSource(config *SourceConfig) ImageSource {
return &S3ImageSource{Config: config}
}
func (s *S3ImageSource) Matches(r *http.Request) bool {
return r.Method == http.MethodGet && parseS3Key(r) != ""
}
func (s *S3ImageSource) GetImage(req *http.Request) ([]byte, error) {
key, bucket, region := parseS3Key(req), parseS3Bucket(req), parseS3Region(req)
session, err := newS3Session(region)
if err != nil {
return nil, fmt.Errorf("failed to create s3 session: %w", err)
}
buffer := aws.NewWriteAtBuffer([]byte{})
buffer.GrowthCoeff = 1.5
if _, err := s3manager.NewDownloader(session).
Download(
buffer,
&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: &key,
}); err != nil {
return nil, fmt.Errorf("failed to download file, %w", err)
}
return buffer.Bytes(), nil
}
func uploadBufferToS3(buffer []byte, outputKey, bucket, region string) error {
sess, err := newS3Session(region)
if err != nil {
return fmt.Errorf("failed to create s3 session: %w", err)
}
meta, err := getMetadata(buffer)
if err != nil {
return fmt.Errorf("failed to get metadata from file: %w", err)
}
if _, err := s3manager.NewUploader(sess).
Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: &outputKey,
Body: bytes.NewReader(buffer),
ContentType: aws.String(http.DetectContentType(buffer)),
Metadata: meta,
}); err != nil {
return fmt.Errorf("failed to upload file, %w", err)
}
return nil
}
func parseS3Key(request *http.Request) string {
return request.URL.Query().Get("s3key")
}
func parseS3OutputKey(request *http.Request) string {
return request.URL.Query().Get("outputKey")
}
func parseS3Bucket(request *http.Request) string {
return request.URL.Query().Get("bucket")
}
func parseS3Region(request *http.Request) string {
return request.URL.Query().Get("region")
}
type S3Source struct {
Zone string
}
func (s *S3Source) DownloadImage(container, imageKey string) ([]byte, error) {
session, err := newS3Session(s.Zone)
if err != nil {
return nil, fmt.Errorf("failed to create s3 session: %w", err)
}
buffer := aws.NewWriteAtBuffer([]byte{})
buffer.GrowthCoeff = 1.5
if _, err := s3manager.NewDownloader(session).
Download(
buffer,
&s3.GetObjectInput{
Bucket: aws.String(container),
Key: &imageKey,
}); err != nil {
return nil, fmt.Errorf("sources3: failed to download image, %w", err)
}
return buffer.Bytes(), nil
}
func (s *S3Source) UploadImage(data []byte, fileKey, container string) error {
sess, err := newS3Session(s.Zone)
if err != nil {
return fmt.Errorf("failed to create s3 session: %w", err)
}
meta, err := getMetadata(data)
if err != nil {
return fmt.Errorf("failed to get metadata from file: %w", err)
}
if _, err := s3manager.NewUploader(sess).
Upload(&s3manager.UploadInput{
Bucket: aws.String(container),
Key: &fileKey,
Body: bytes.NewReader(data),
ContentType: aws.String(http.DetectContentType(data)),
Metadata: meta,
}); err != nil {
return fmt.Errorf("failed to upload file, %w", err)
}
return nil
}
func getMetadata(buffer []byte) (map[string]*string, error) {
if filetype.IsImage(buffer) {
meta, err := bimg.Metadata(buffer)
if err != nil {
return nil, NewError("Cannot retrieve image metadata: %s"+err.Error(), BadRequest)
}
return map[string]*string{
"width": aws.String(strconv.Itoa(meta.Size.Width)),
"height": aws.String(strconv.Itoa(meta.Size.Height)),
}, nil
}
return make(map[string]*string), nil
}