forked from h2non/imaginary
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsource_aws_s3.go
More file actions
66 lines (53 loc) · 1.4 KB
/
source_aws_s3.go
File metadata and controls
66 lines (53 loc) · 1.4 KB
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
package main
import (
"io/ioutil"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/kumparan/imaginary/config"
log "github.com/sirupsen/logrus"
)
const ImageSourceTypeS3 ImageSourceType = "s3"
const S3QueryKey = "s3"
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 && r.URL.Query().Get(S3QueryKey) != ""
}
func (s *S3ImageSource) GetImage(req *http.Request) ([]byte, error) {
awsS3Key := parseS3Path(req)
return s.fetchImage(awsS3Key, req)
}
func (s *S3ImageSource) fetchImage(awsS3Key string, ireq *http.Request) (b []byte, err error) {
// Check remote image size by fetching HTTP Headers
result, err := s.Config.S3Client.GetObject(&s3.GetObjectInput{
Bucket: aws.String(config.AWSS3Bucket()),
Key: aws.String(awsS3Key),
})
if err != nil {
log.WithFields(log.Fields{
"awsS3Key": awsS3Key}).
Info(err)
return nil, err
}
b, err = ioutil.ReadAll(result.Body)
if err != nil {
log.WithFields(log.Fields{
"awsS3Key": awsS3Key}).
Error(err)
return nil, err
}
return
}
func parseS3Path(request *http.Request) (awsS3Key string) {
return request.URL.Query().Get(S3QueryKey)
}
func init() {
RegisterSource(ImageSourceTypeS3, NewS3ImageSource)
}