-
Notifications
You must be signed in to change notification settings - Fork 496
/
s3.go
269 lines (240 loc) · 6.03 KB
/
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package storage
import (
"bytes"
"context"
"io"
"net/http"
"os"
"path"
log "github.com/sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/stellar/go/support/errors"
)
type s3HttpProxy interface {
Send(*s3.GetObjectInput) (io.ReadCloser, error)
}
type defaultS3HttpProxy struct {
*S3Storage
}
func (proxy *defaultS3HttpProxy) Send(params *s3.GetObjectInput) (io.ReadCloser, error) {
req, resp := proxy.svc.GetObjectRequest(params)
if proxy.unsignedRequests {
req.Handlers.Sign.Clear() // makes this request unsigned
}
req.SetContext(proxy.ctx)
logReq(req.HTTPRequest)
err := req.Send()
logResp(req.HTTPResponse)
return resp.Body, err
}
type S3Storage struct {
ctx context.Context
svc s3iface.S3API
bucket string
prefix string
unsignedRequests bool
writeACLrule string
s3Http s3HttpProxy
}
func NewS3Storage(
ctx context.Context,
bucket string,
prefix string,
region string,
endpoint string,
unsignedRequests bool,
writeACLrule string,
) (Storage, error) {
log.WithFields(log.Fields{"bucket": bucket,
"prefix": prefix,
"region": region,
"endpoint": endpoint}).Debug("s3: making backend")
cfg := &aws.Config{
Region: aws.String(region),
Endpoint: aws.String(endpoint),
}
cfg = cfg.WithS3ForcePathStyle(true)
sess, err := session.NewSession(cfg)
if err != nil {
return nil, err
}
backend := S3Storage{
ctx: ctx,
svc: s3.New(sess),
bucket: bucket,
prefix: prefix,
unsignedRequests: unsignedRequests,
writeACLrule: writeACLrule,
}
return &backend, nil
}
func (b *S3Storage) GetFile(pth string) (io.ReadCloser, error) {
key := path.Join(b.prefix, pth)
params := &s3.GetObjectInput{
Bucket: aws.String(b.bucket),
Key: aws.String(key),
}
resp, err := b.s3HttpProxy().Send(params)
if err != nil {
if aerr, ok := err.(awserr.Error); ok && aerr.Code() == s3.ErrCodeNoSuchKey {
return nil, os.ErrNotExist
}
return nil, err
}
return resp, nil
}
func (b *S3Storage) Head(pth string) (*http.Response, error) {
key := path.Join(b.prefix, pth)
params := &s3.HeadObjectInput{
Bucket: aws.String(b.bucket),
Key: aws.String(key),
}
req, _ := b.svc.HeadObjectRequest(params)
if b.unsignedRequests {
req.Handlers.Sign.Clear() // makes this request unsigned
}
req.SetContext(b.ctx)
logReq(req.HTTPRequest)
err := req.Send()
logResp(req.HTTPResponse)
if req.HTTPResponse != nil && req.HTTPResponse.StatusCode == http.StatusNotFound {
// Lately the S3 SDK has started treating a 404 as generating a non-nil
// 'err', so we have to test for this _before_ we test 'err' for
// nil-ness. This is undocumented, as is the err.Code returned in that
// error ("NotFound"), and it's a breaking change from what it used to
// do, and not what one would expect, but who's counting? We'll just
// turn it _back_ into what it used to be: 404 as a non-erroneously
// received HTTP response.
return req.HTTPResponse, nil
}
if err != nil {
return nil, err
}
return req.HTTPResponse, nil
}
func (b *S3Storage) Exists(pth string) (bool, error) {
resp, err := b.Head(pth)
if err != nil {
return false, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 400 {
return true, nil
} else if resp.StatusCode == http.StatusNotFound {
return false, nil
} else {
return false, errors.Errorf("Unknown status code=%d", resp.StatusCode)
}
}
func (b *S3Storage) Size(pth string) (int64, error) {
resp, err := b.Head(pth)
if err != nil {
return 0, err
}
if resp.StatusCode >= 200 && resp.StatusCode < 400 {
return resp.ContentLength, nil
} else if resp.StatusCode == http.StatusNotFound {
return 0, nil
} else {
return 0, errors.Errorf("Unknown status code=%d", resp.StatusCode)
}
}
func (b *S3Storage) GetACLWriteRule() string {
if b.writeACLrule == "" {
return s3.ObjectCannedACLPublicRead
}
return b.writeACLrule
}
func (b *S3Storage) PutFile(pth string, in io.ReadCloser) error {
var buf bytes.Buffer
_, err := buf.ReadFrom(in)
in.Close()
if err != nil {
return err
}
key := path.Join(b.prefix, pth)
params := &s3.PutObjectInput{
Bucket: aws.String(b.bucket),
Key: aws.String(key),
ACL: aws.String(b.GetACLWriteRule()),
Body: bytes.NewReader(buf.Bytes()),
}
req, _ := b.svc.PutObjectRequest(params)
if b.unsignedRequests {
req.Handlers.Sign.Clear() // makes this request unsigned
}
req.SetContext(b.ctx)
logReq(req.HTTPRequest)
err = req.Send()
logResp(req.HTTPResponse)
in.Close()
return err
}
func (b *S3Storage) ListFiles(pth string) (chan string, chan error) {
prefix := path.Join(b.prefix, pth)
ch := make(chan string)
errs := make(chan error)
params := &s3.ListObjectsInput{
Bucket: aws.String(b.bucket),
MaxKeys: aws.Int64(1000),
Prefix: aws.String(prefix),
}
req, resp := b.svc.ListObjectsRequest(params)
if b.unsignedRequests {
req.Handlers.Sign.Clear() // makes this request unsigned
}
req.SetContext(b.ctx)
logReq(req.HTTPRequest)
err := req.Send()
logResp(req.HTTPResponse)
if err != nil {
errs <- err
close(ch)
close(errs)
return ch, errs
}
go func() {
for {
for _, c := range resp.Contents {
params.Marker = c.Key
log.WithField("key", *c.Key).Trace("s3: ListFiles")
ch <- *c.Key
}
if *resp.IsTruncated {
req, resp = b.svc.ListObjectsRequest(params)
if b.unsignedRequests {
req.Handlers.Sign.Clear() // makes this request unsigned
}
req.SetContext(b.ctx)
logReq(req.HTTPRequest)
err := req.Send()
logResp(req.HTTPResponse)
if err != nil {
errs <- err
}
} else {
break
}
}
close(ch)
close(errs)
}()
return ch, errs
}
func (b *S3Storage) CanListFiles() bool {
return true
}
func (b *S3Storage) Close() error {
return nil
}
func (b *S3Storage) s3HttpProxy() s3HttpProxy {
if b.s3Http != nil {
return b.s3Http
}
return &defaultS3HttpProxy{
S3Storage: b,
}
}