This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
/
loader_s3.go
78 lines (64 loc) · 1.7 KB
/
loader_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
package app
import (
"context"
"io/ioutil"
"net/url"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/s3iface"
"github.com/pkg/errors"
)
// S3Loader is a FileLoader that loads files from S3.
type S3Loader struct {
s3 s3iface.ClientAPI
}
// Load implements FileLoader.Load.
func (l S3Loader) Load(url *url.URL) ([]byte, error) {
if url.Scheme != "s3" {
return nil, errors.Errorf("invalid scheme: %s", url.Scheme)
}
if url.Host == "" {
return nil, errors.New("missing bucket")
}
if url.Path == "" {
return nil, errors.New("missing key")
}
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Minute)
defer cancelFunc()
resp, err := l.s3.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(url.Host),
// The path component of a URL includes the prefixed '/'.
// However, despite S3 allowing URL's in raw APIs, it does _not_ expect
// it in many SDKs. It would be nice if they handled it more consistently,
// but here we are.
Key: aws.String(url.Path[1:]),
}).Send(ctx)
if err != nil {
return nil, errors.Wrapf(err, "failed to load %s", url.String())
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
func init() {
var init sync.Once
var loader FileLoader
var initErr error
ctr := func() (FileLoader, error) {
init.Do(func() {
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
initErr = errors.Wrap(err, "failed to initialize S3Loader")
return
}
loader = &S3Loader{s3: s3.New(cfg)}
})
if initErr != nil {
return nil, initErr
}
return loader, nil
}
RegisterFileLoaderCtor("s3", ctr)
}