-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.go
61 lines (51 loc) · 1.18 KB
/
get.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
package s3
import (
"context"
"io"
"io/ioutil"
"net/url"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/pkg/errors"
"github.com/rai-project/store"
)
func (s *s3Client) Get(key0 string, opts ...store.GetOption) ([]byte, error) {
body, err := s.GetReader(key0, opts...)
if err != nil {
return nil, err
}
if body != nil {
defer body.Close()
}
ret, err := ioutil.ReadAll(body)
if err != nil {
return nil, err
}
return ret, nil
}
func (s *s3Client) GetReader(key0 string, opts ...store.GetOption) (io.ReadCloser, error) {
options := store.GetOptions{
Context: context.Background(),
}
for _, o := range opts {
o(&options)
}
prefix := s.opts.BaseURL + "/" + s.opts.Bucket + "/"
key0, err := cleanupKey(key0, prefix)
if err != nil {
return nil, err
}
key, err := url.QueryUnescape(key0)
if err != nil {
log.WithField("key", key0).Error("Failed to unescape ", key0)
key = key0
}
obj, err := s.client.GetObject(&s3.GetObjectInput{
Bucket: aws.String(s.opts.Bucket),
Key: aws.String(key),
})
if err != nil {
return nil, errors.Wrapf(err, "Failed to get data from %s/%s\n", s.opts.Bucket, key)
}
return obj.Body, nil
}