forked from ipfs/go-ds-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
395 lines (337 loc) · 8.88 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package s3ds
import (
"bytes"
"context"
"fmt"
"io"
"path"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/credentials/endpointcreds"
"github.com/aws/aws-sdk-go/aws/defaults"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
ds "github.com/ipfs/go-datastore"
dsq "github.com/ipfs/go-datastore/query"
)
const (
// listMax is the largest amount of objects you can request from S3 in a list
// call.
listMax = 1000
// deleteMax is the largest amount of objects you can delete from S3 in a
// delete objects call.
deleteMax = 1000
defaultWorkers = 100
// credsRefreshWindow, subtracted from the endpointcred's expiration time, is the
// earliest time the endpoint creds can be refreshed.
credsRefreshWindow = 2 * time.Minute
)
var _ ds.Datastore = (*S3Bucket)(nil)
type S3Bucket struct {
Config
S3 *s3.S3
}
type Config struct {
AccessKey string
SecretKey string
SessionToken string
Bucket string
Region string
RegionEndpoint string
RootDirectory string
Workers int
CredentialsEndpoint string
}
func NewS3Datastore(conf Config) (*S3Bucket, error) {
if conf.Workers == 0 {
conf.Workers = defaultWorkers
}
awsConfig := aws.NewConfig()
sess, err := session.NewSession()
if err != nil {
return nil, fmt.Errorf("failed to create new session: %s", err)
}
d := defaults.Get()
creds := credentials.NewChainCredentials([]credentials.Provider{
&credentials.StaticProvider{Value: credentials.Value{
AccessKeyID: conf.AccessKey,
SecretAccessKey: conf.SecretKey,
SessionToken: conf.SessionToken,
}},
&credentials.EnvProvider{},
&credentials.SharedCredentialsProvider{},
&ec2rolecreds.EC2RoleProvider{Client: ec2metadata.New(sess)},
endpointcreds.NewProviderClient(*d.Config, d.Handlers, conf.CredentialsEndpoint,
func(p *endpointcreds.Provider) { p.ExpiryWindow = credsRefreshWindow },
),
})
if conf.RegionEndpoint != "" {
awsConfig.WithS3ForcePathStyle(true)
awsConfig.WithEndpoint(conf.RegionEndpoint)
}
awsConfig.WithCredentials(creds)
awsConfig.CredentialsChainVerboseErrors = aws.Bool(true)
awsConfig.WithRegion(conf.Region)
sess, err = session.NewSession(awsConfig)
if err != nil {
return nil, fmt.Errorf("failed to create new session with aws config: %s", err)
}
s3obj := s3.New(sess)
return &S3Bucket{
S3: s3obj,
Config: conf,
}, nil
}
func (s *S3Bucket) Put(ctx context.Context, k ds.Key, value []byte) error {
_, err := s.S3.PutObjectWithContext(ctx, &s3.PutObjectInput{
Bucket: aws.String(s.Bucket),
Key: aws.String(s.s3Path(k.String())),
Body: bytes.NewReader(value),
})
return err
}
func (s *S3Bucket) Sync(ctx context.Context, prefix ds.Key) error {
return nil
}
func (s *S3Bucket) Get(ctx context.Context, k ds.Key) ([]byte, error) {
resp, err := s.S3.GetObjectWithContext(ctx, &s3.GetObjectInput{
Bucket: aws.String(s.Bucket),
Key: aws.String(s.s3Path(k.String())),
})
if err != nil {
if isNotFound(err) {
return nil, ds.ErrNotFound
}
return nil, err
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
}
func (s *S3Bucket) Has(ctx context.Context, k ds.Key) (exists bool, err error) {
_, err = s.GetSize(ctx, k)
if err != nil {
if err == ds.ErrNotFound {
return false, nil
}
return false, err
}
return true, nil
}
func (s *S3Bucket) GetSize(ctx context.Context, k ds.Key) (size int, err error) {
resp, err := s.S3.HeadObjectWithContext(ctx, &s3.HeadObjectInput{
Bucket: aws.String(s.Bucket),
Key: aws.String(s.s3Path(k.String())),
})
if err != nil {
if s3Err, ok := err.(awserr.Error); ok && s3Err.Code() == "NotFound" {
return -1, ds.ErrNotFound
}
return -1, err
}
return int(*resp.ContentLength), nil
}
func (s *S3Bucket) Delete(ctx context.Context, k ds.Key) error {
_, err := s.S3.DeleteObjectWithContext(ctx, &s3.DeleteObjectInput{
Bucket: aws.String(s.Bucket),
Key: aws.String(s.s3Path(k.String())),
})
if isNotFound(err) {
// delete is idempotent
err = nil
}
return err
}
func (s *S3Bucket) Query(ctx context.Context, q dsq.Query) (dsq.Results, error) {
if q.Orders != nil || q.Filters != nil {
return nil, fmt.Errorf("s3ds: filters or orders are not supported")
}
// S3 store a "/foo" key as "foo" so we need to trim the leading "/"
q.Prefix = strings.TrimPrefix(q.Prefix, "/")
limit := q.Limit + q.Offset
if limit == 0 || limit > listMax {
limit = listMax
}
resp, err := s.S3.ListObjectsV2WithContext(ctx, &s3.ListObjectsV2Input{
Bucket: aws.String(s.Bucket),
Prefix: aws.String(s.s3Path(q.Prefix)),
MaxKeys: aws.Int64(int64(limit)),
})
if err != nil {
return nil, err
}
index := q.Offset
nextValue := func() (dsq.Result, bool) {
for index >= len(resp.Contents) {
if !*resp.IsTruncated {
return dsq.Result{}, false
}
index -= len(resp.Contents)
resp, err = s.S3.ListObjectsV2WithContext(ctx, &s3.ListObjectsV2Input{
Bucket: aws.String(s.Bucket),
Prefix: aws.String(s.s3Path(q.Prefix)),
Delimiter: aws.String("/"),
MaxKeys: aws.Int64(listMax),
ContinuationToken: resp.NextContinuationToken,
})
if err != nil {
return dsq.Result{Error: err}, false
}
}
entry := dsq.Entry{
Key: ds.NewKey(*resp.Contents[index].Key).String(),
Size: int(*resp.Contents[index].Size),
}
if !q.KeysOnly {
value, err := s.Get(ctx, ds.NewKey(entry.Key))
if err != nil {
return dsq.Result{Error: err}, false
}
entry.Value = value
}
index++
return dsq.Result{Entry: entry}, true
}
return dsq.ResultsFromIterator(q, dsq.Iterator{
Close: func() error {
return nil
},
Next: nextValue,
}), nil
}
func (s *S3Bucket) Batch(_ context.Context) (ds.Batch, error) {
return &s3Batch{
s: s,
ops: make(map[string]batchOp),
numWorkers: s.Workers,
}, nil
}
func (s *S3Bucket) Close() error {
return nil
}
func (s *S3Bucket) s3Path(p string) string {
return path.Join(s.RootDirectory, p)
}
func isNotFound(err error) bool {
s3Err, ok := err.(awserr.Error)
return ok && s3Err.Code() == s3.ErrCodeNoSuchKey
}
type s3Batch struct {
s *S3Bucket
ops map[string]batchOp
numWorkers int
}
type batchOp struct {
val []byte
delete bool
}
func (b *s3Batch) Put(ctx context.Context, k ds.Key, val []byte) error {
b.ops[k.String()] = batchOp{
val: val,
delete: false,
}
return nil
}
func (b *s3Batch) Delete(ctx context.Context, k ds.Key) error {
b.ops[k.String()] = batchOp{
val: nil,
delete: true,
}
return nil
}
func (b *s3Batch) Commit(ctx context.Context) error {
var (
deleteObjs []*s3.ObjectIdentifier
putKeys []ds.Key
)
for k, op := range b.ops {
if op.delete {
deleteObjs = append(deleteObjs, &s3.ObjectIdentifier{
Key: aws.String(k),
})
} else {
putKeys = append(putKeys, ds.NewKey(k))
}
}
numJobs := len(putKeys) + (len(deleteObjs) / deleteMax)
jobs := make(chan func() error, numJobs)
results := make(chan error, numJobs)
numWorkers := b.numWorkers
if numJobs < numWorkers {
numWorkers = numJobs
}
var wg sync.WaitGroup
wg.Add(numWorkers)
defer wg.Wait()
for w := 0; w < numWorkers; w++ {
go func() {
defer wg.Done()
worker(jobs, results)
}()
}
for _, k := range putKeys {
jobs <- b.newPutJob(ctx, k, b.ops[k.String()].val)
}
if len(deleteObjs) > 0 {
for i := 0; i < len(deleteObjs); i += deleteMax {
limit := deleteMax
if len(deleteObjs[i:]) < limit {
limit = len(deleteObjs[i:])
}
jobs <- b.newDeleteJob(ctx, deleteObjs[i:i+limit])
}
}
close(jobs)
var errs []string
for i := 0; i < numJobs; i++ {
err := <-results
if err != nil {
errs = append(errs, err.Error())
}
}
if len(errs) > 0 {
return fmt.Errorf("s3ds: failed batch operation:\n%s", strings.Join(errs, "\n"))
}
return nil
}
func (b *s3Batch) newPutJob(ctx context.Context, k ds.Key, value []byte) func() error {
return func() error {
return b.s.Put(ctx, k, value)
}
}
func (b *s3Batch) newDeleteJob(ctx context.Context, objs []*s3.ObjectIdentifier) func() error {
return func() error {
resp, err := b.s.S3.DeleteObjectsWithContext(ctx, &s3.DeleteObjectsInput{
Bucket: aws.String(b.s.Bucket),
Delete: &s3.Delete{
Objects: objs,
},
})
if err != nil && !isNotFound(err) {
return err
}
var errs []string
for _, err := range resp.Errors {
if err.Code != nil && *err.Code == s3.ErrCodeNoSuchKey {
// idempotent
continue
}
errs = append(errs, err.String())
}
if len(errs) > 0 {
return fmt.Errorf("failed to delete objects: %s", errs)
}
return nil
}
}
func worker(jobs <-chan func() error, results chan<- error) {
for j := range jobs {
results <- j()
}
}
var _ ds.Batching = (*S3Bucket)(nil)