forked from lytics/cloudstorage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
743 lines (628 loc) · 18.4 KB
/
store.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
package awss3
import (
"fmt"
"io"
"net/http"
"os"
"path"
"strings"
"sync"
"time"
"github.com/araddon/gou"
"github.com/pborman/uuid"
"golang.org/x/net/context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/gocontrib/cloudstorage"
"github.com/gocontrib/cloudstorage/csbufio"
)
const (
// StoreType = "s3" this is used to define the storage type to create
// from cloudstorage.NewStore(config)
StoreType = "s3"
// Configuration Keys. These are the names of keys
// to look for in the json map[string]string to extract for config.
// ConfKeyAccessKey config key name of the aws access_key(id) for auth
ConfKeyAccessKey = "access_key"
// ConfKeyAccessSecret config key name of the aws acccess secret
ConfKeyAccessSecret = "access_secret"
// ConfKeyARN config key name of the aws ARN name of user
ConfKeyARN = "arn"
// ConfKeyDisableSSL config key name of disabling ssl flag
ConfKeyDisableSSL = "disable_ssl"
// ConfKeyDebugLog config key to enable LogDebug log level
ConfKeyDebugLog = "debug_log"
// Authentication Source's
// AuthAccessKey is for using aws access key/secret pairs
AuthAccessKey cloudstorage.AuthMethod = "aws_access_key"
)
var (
// Retries number of times to retry upon failures.
Retries = 3
// PageSize is default page size
PageSize = 2000
// ErrNoS3Session no valid session
ErrNoS3Session = fmt.Errorf("no valid aws session was created")
// ErrNoAccessKey error for no access_key
ErrNoAccessKey = fmt.Errorf("no settings.access_key")
// ErrNoAccessSecret error for no settings.access_secret
ErrNoAccessSecret = fmt.Errorf("no settings.access_secret")
// ErrNoAuth error for no findable auth
ErrNoAuth = fmt.Errorf("No auth provided")
)
func init() {
// Register this Driver (s3) in cloudstorage driver registry.
cloudstorage.Register(StoreType, func(conf *cloudstorage.Config) (cloudstorage.Store, error) {
client, sess, err := NewClient(conf)
if err != nil {
return nil, err
}
return NewStore(client, sess, conf)
})
}
type (
// FS Simple wrapper for accessing s3 files, it doesn't currently implement a
// Reader/Writer interface so not useful for stream reading of large files yet.
FS struct {
PageSize int
ID string
client *s3.S3
sess *session.Session
endpoint string
bucket string
cachepath string
}
object struct {
fs *FS
o *s3.GetObjectOutput
cachedcopy *os.File
name string // aka "key" in s3
updated time.Time // LastModifyied in s3
metadata map[string]string
bucket string
readonly bool
opened bool
cachepath string
infoOnce sync.Once
infoErr error
}
)
// NewClient create new AWS s3 Client. Uses cloudstorage.Config to read
// necessary config settings such as bucket, region, auth.
func NewClient(conf *cloudstorage.Config) (*s3.S3, *session.Session, error) {
awsConf, err := makeAWSConf(conf)
if err != nil {
return nil, nil, err
}
sess := session.New(awsConf)
if sess == nil {
return nil, nil, ErrNoS3Session
}
s3Client := s3.New(sess)
ensureBucket(s3Client, conf.Bucket)
return s3Client, sess, nil
}
func makeAWSConf(conf *cloudstorage.Config) (*aws.Config, error) {
awsConf := aws.NewConfig().
WithHTTPClient(http.DefaultClient).
WithMaxRetries(aws.UseServiceDefaultRetries).
WithLogger(aws.NewDefaultLogger()).
WithLogLevel(aws.LogOff).
WithSleepDelay(time.Sleep)
if conf.Region != "" {
awsConf.WithRegion(conf.Region)
} else {
awsConf.WithRegion("us-east-1")
}
switch conf.AuthMethod {
case AuthAccessKey:
accessKey := conf.Settings.String(ConfKeyAccessKey)
if accessKey == "" {
return nil, ErrNoAccessKey
}
secretKey := conf.Settings.String(ConfKeyAccessSecret)
if secretKey == "" {
return nil, ErrNoAccessSecret
}
awsConf.WithCredentials(credentials.NewStaticCredentials(accessKey, secretKey, ""))
default:
return nil, ErrNoAuth
}
if conf.BaseUrl != "" {
awsConf.WithEndpoint(conf.BaseUrl).WithS3ForcePathStyle(true)
}
if conf.Settings.Bool(ConfKeyDebugLog) {
awsConf.WithLogLevel(aws.LogDebug)
}
disableSSL := conf.Settings.Bool(ConfKeyDisableSSL)
if disableSSL {
awsConf.WithDisableSSL(true)
}
return awsConf, nil
}
func ensureBucket(s3Client *s3.S3, bucket string) error {
_, err := s3Client.CreateBucket(&s3.CreateBucketInput{
Bucket: aws.String(bucket),
})
return err
}
// NewStore Create AWS S3 storage client of type cloudstorage.Store
func NewStore(c *s3.S3, sess *session.Session, conf *cloudstorage.Config) (*FS, error) {
if conf.TmpDir == "" {
return nil, fmt.Errorf("unable to create cachepath. config.tmpdir=%q", conf.TmpDir)
}
err := os.MkdirAll(conf.TmpDir, 0775)
if err != nil {
return nil, fmt.Errorf("unable to create cachepath. config.tmpdir=%q err=%v", conf.TmpDir, err)
}
uid := uuid.NewUUID().String()
uid = strings.Replace(uid, "-", "", -1)
return &FS{
client: c,
sess: sess,
bucket: conf.Bucket,
cachepath: conf.TmpDir,
ID: uid,
PageSize: cloudstorage.MaxResults,
}, nil
}
// Type of store = "s3"
func (f *FS) Type() string {
return StoreType
}
// Client gets access to the underlying s3 cloud storage client.
func (f *FS) Client() interface{} {
return f.client
}
// String function to provide s3://..../file path
func (f *FS) String() string {
return fmt.Sprintf("s3://%s/", f.bucket)
}
// NewObject of Type s3.
func (f *FS) NewObject(objectname string) (cloudstorage.Object, error) {
obj, err := f.Get(context.Background(), objectname)
if err != nil && err != cloudstorage.ErrObjectNotFound {
return nil, err
} else if obj != nil {
return nil, cloudstorage.ErrObjectExists
}
cf := cloudstorage.CachePathObj(f.cachepath, objectname, f.ID)
return &object{
fs: f,
name: objectname,
metadata: map[string]string{cloudstorage.ContentTypeKey: cloudstorage.ContentType(objectname)},
bucket: f.bucket,
cachedcopy: nil,
cachepath: cf,
}, nil
}
// Get a single File Object
func (f *FS) Get(ctx context.Context, objectpath string) (cloudstorage.Object, error) {
obj, err := f.getObjectMeta(ctx, objectpath)
if err != nil {
return nil, err
} else if obj == nil {
return nil, cloudstorage.ErrObjectNotFound
}
return obj, nil
}
// get single object
func (f *FS) getObjectMeta(ctx context.Context, objectname string) (*object, error) {
req := &s3.HeadObjectInput{
Key: aws.String(objectname),
Bucket: aws.String(f.bucket),
}
res, err := f.client.HeadObjectWithContext(ctx, req)
if err != nil {
// translate the string error to typed error
if strings.Contains(err.Error(), "Not Found") {
return nil, cloudstorage.ErrObjectNotFound
}
return nil, err
}
return newObjectFromHead(f, objectname, res), nil
}
func (f *FS) getS3OpenObject(ctx context.Context, objectname string) (*s3.GetObjectOutput, error) {
res, err := f.client.GetObjectWithContext(ctx, &s3.GetObjectInput{
Key: aws.String(objectname),
Bucket: aws.String(f.bucket),
})
if err != nil {
// translate the string error to typed error
if strings.Contains(err.Error(), "NoSuchKey") {
return nil, cloudstorage.ErrObjectNotFound
}
return nil, err
}
return res, nil
}
func convertMetaData(m map[string]*string) (map[string]string, error) {
result := make(map[string]string, len(m))
for key, value := range m {
if value != nil {
result[strings.ToLower(key)] = *value
} else {
result[strings.ToLower(key)] = ""
}
}
return result, nil
}
// List objects from this store.
func (f *FS) List(ctx context.Context, q cloudstorage.Query) (*cloudstorage.ObjectsResponse, error) {
itemLimit := int64(f.PageSize)
if q.PageSize > 0 {
itemLimit = int64(q.PageSize)
}
params := &s3.ListObjectsInput{
Bucket: aws.String(f.bucket),
Marker: &q.Marker,
MaxKeys: &itemLimit,
Prefix: &q.Prefix,
}
resp, err := f.client.ListObjects(params)
if err != nil {
gou.Warnf("err = %v", err)
return nil, err
}
objResp := &cloudstorage.ObjectsResponse{
Objects: make(cloudstorage.Objects, len(resp.Contents)),
}
for i, o := range resp.Contents {
objResp.Objects[i] = newObject(f, o)
}
if resp.IsTruncated != nil && *resp.IsTruncated {
lastObj := *resp.Contents[len(resp.Contents)-1].Key
objResp.NextMarker = lastObj
}
return objResp, nil
}
// Objects returns an iterator over the objects in the s3 bucket that match the Query q.
// If q is nil, no filtering is done.
func (f *FS) Objects(ctx context.Context, q cloudstorage.Query) (cloudstorage.ObjectIterator, error) {
return cloudstorage.NewObjectPageIterator(ctx, f, q), nil
}
// Folders get folders list.
func (f *FS) Folders(ctx context.Context, q cloudstorage.Query) ([]string, error) {
q.Delimiter = "/"
// Think we should just put 1 here right?
itemLimit := int64(f.PageSize)
if q.PageSize > 0 {
itemLimit = int64(q.PageSize)
}
params := &s3.ListObjectsInput{
Bucket: aws.String(f.bucket),
MaxKeys: &itemLimit,
Prefix: &q.Prefix,
Delimiter: &q.Delimiter,
}
folders := make([]string, 0)
for {
select {
case <-ctx.Done():
// If has been closed
return folders, ctx.Err()
default:
if q.Marker != "" {
params.Marker = &q.Marker
}
resp, err := f.client.ListObjectsWithContext(ctx, params)
if err != nil {
return nil, err
}
for _, cp := range resp.CommonPrefixes {
folders = append(folders, strings.TrimPrefix(*cp.Prefix, `/`))
}
return folders, nil
}
}
}
/*
// Copy from src to destination
func (f *FS) Copy(ctx context.Context, src, des cloudstorage.Object) error {
so, ok := src.(*object)
if !ok {
return fmt.Errorf("Copy source file expected s3 but got %T", src)
}
do, ok := des.(*object)
if !ok {
return fmt.Errorf("Copy destination expected s3 but got %T", des)
}
oh := so.b.Object(so.name)
dh := do.b.Object(do.name)
_, err := dh.CopierFrom(oh).Run(ctx)
return err
}
// Move which is a Copy & Delete
func (f *FS) Move(ctx context.Context, src, des cloudstorage.Object) error {
so, ok := src.(*object)
if !ok {
return fmt.Errorf("Move source file expected s3 but got %T", src)
}
do, ok := des.(*object)
if !ok {
return fmt.Errorf("Move destination expected s3 but got %T", des)
}
oh := so.b.Object(so.name)
dh := do.b.Object(des.name)
if _, err := dh.CopierFrom(oh).Run(ctx); err != nil {
return err
}
return oh.Delete(ctx)
}
*/
// NewReader create file reader.
func (f *FS) NewReader(o string) (io.ReadCloser, error) {
return f.NewReaderWithContext(context.Background(), o)
}
// NewReaderWithContext create new File reader with context.
func (f *FS) NewReaderWithContext(ctx context.Context, objectname string) (io.ReadCloser, error) {
res, err := f.client.GetObjectWithContext(ctx, &s3.GetObjectInput{
Key: aws.String(objectname),
Bucket: aws.String(f.bucket),
})
if err != nil {
// translate the string error to typed error
if strings.Contains(err.Error(), "NoSuchKey") {
return nil, cloudstorage.ErrObjectNotFound
}
return nil, err
}
return res.Body, nil
}
// NewWriter create Object Writer.
func (f *FS) NewWriter(objectName string, metadata map[string]string) (io.WriteCloser, error) {
return f.NewWriterWithContext(context.Background(), objectName, metadata)
}
// NewWriterWithContext create writer with provided context and metadata.
func (f *FS) NewWriterWithContext(ctx context.Context, objectName string, metadata map[string]string, opts ...cloudstorage.Opts) (io.WriteCloser, error) {
if len(opts) > 0 && opts[0].IfNotExists {
return nil, fmt.Errorf("options IfNotExists not supported for store type")
}
// Create an uploader with the session and default options
uploader := s3manager.NewUploader(f.sess)
pr, pw := io.Pipe()
bw := csbufio.NewWriter(pw)
go func() {
// TODO: this needs to be managed, ie shutdown signals, close, handler err etc.
// Upload the file to S3.
_, err := uploader.UploadWithContext(ctx, &s3manager.UploadInput{
Bucket: aws.String(f.bucket),
Key: aws.String(objectName),
Body: pr,
})
if err != nil {
gou.Warnf("could not upload %v", err)
}
}()
return bw, nil
}
// Delete requested object path string.
func (f *FS) Delete(ctx context.Context, obj string) error {
params := &s3.DeleteObjectInput{
Bucket: aws.String(f.bucket),
Key: aws.String(obj),
}
_, err := f.client.DeleteObjectWithContext(ctx, params)
if err != nil {
return err
}
return nil
}
func newObject(f *FS, o *s3.Object) *object {
obj := &object{
fs: f,
name: *o.Key,
bucket: f.bucket,
cachepath: cloudstorage.CachePathObj(f.cachepath, *o.Key, f.ID),
}
if o.LastModified != nil {
obj.updated = *o.LastModified
}
return obj
}
func newObjectFromHead(f *FS, name string, o *s3.HeadObjectOutput) *object {
obj := &object{
fs: f,
name: name,
bucket: f.bucket,
cachepath: cloudstorage.CachePathObj(f.cachepath, name, f.ID),
}
if o.LastModified != nil {
obj.updated = *o.LastModified
}
// metadata?
obj.metadata, _ = convertMetaData(o.Metadata)
return obj
}
func (o *object) StorageSource() string {
return StoreType
}
func (o *object) Name() string {
return o.name
}
func (o *object) String() string {
return o.name
}
func (o *object) Updated() time.Time {
return o.updated
}
func (o *object) MetaData() map[string]string {
return o.metadata
}
func (o *object) SetMetaData(meta map[string]string) {
o.metadata = meta
}
func (o *object) Delete() error {
return o.fs.Delete(context.Background(), o.name)
}
func (o *object) Open(accesslevel cloudstorage.AccessLevel) (*os.File, error) {
if o.opened {
return nil, fmt.Errorf("the store object is already opened. %s", o.name)
}
var errs []error = make([]error, 0)
var cachedcopy *os.File = nil
var err error
var readonly = accesslevel == cloudstorage.ReadOnly
err = os.MkdirAll(path.Dir(o.cachepath), 0775)
if err != nil {
return nil, fmt.Errorf("error occurred creating cachedcopy dir. cachepath=%s object=%s err=%v", o.cachepath, o.name, err)
}
err = cloudstorage.EnsureDir(o.cachepath)
if err != nil {
return nil, fmt.Errorf("error occurred creating cachedcopy's dir. cachepath=%s err=%v", o.cachepath, err)
}
cachedcopy, err = os.Create(o.cachepath)
if err != nil {
return nil, fmt.Errorf("error occurred creating file. local=%s err=%v", o.cachepath, err)
}
for try := 0; try < Retries; try++ {
if o.o == nil {
obj, err := o.fs.getS3OpenObject(context.Background(), o.name)
if err != nil {
if err == cloudstorage.ErrObjectNotFound {
// New, this is fine
} else {
// lets re-try
errs = append(errs, fmt.Errorf("error getting object err=%v", err))
cloudstorage.Backoff(try)
continue
}
}
if obj != nil {
o.o = obj
}
}
if o.o != nil {
// we have a preexisting object, so lets download it..
defer o.o.Body.Close()
if _, err := cachedcopy.Seek(0, os.SEEK_SET); err != nil {
return nil, fmt.Errorf("error seeking to start of cachedcopy err=%v", err) //don't retry on local fs errors
}
_, err = io.Copy(cachedcopy, o.o.Body)
if err != nil {
errs = append(errs, fmt.Errorf("error coping bytes. err=%v", err))
//recreate the cachedcopy file incase it has incomplete data
if err := os.Remove(o.cachepath); err != nil {
return nil, fmt.Errorf("error resetting the cachedcopy err=%v", err) //don't retry on local fs errors
}
if cachedcopy, err = os.Create(o.cachepath); err != nil {
return nil, fmt.Errorf("error creating a new cachedcopy file. local=%s err=%v", o.cachepath, err)
}
cloudstorage.Backoff(try)
continue
}
}
if readonly {
cachedcopy.Close()
cachedcopy, err = os.Open(o.cachepath)
if err != nil {
name := "unknown"
if cachedcopy != nil {
name = cachedcopy.Name()
}
return nil, fmt.Errorf("error opening file. local=%s object=%s tfile=%v err=%v", o.cachepath, o.name, name, err)
}
} else {
if _, err := cachedcopy.Seek(0, os.SEEK_SET); err != nil {
return nil, fmt.Errorf("error seeking to start of cachedcopy err=%v", err) //don't retry on local fs errors
}
}
o.cachedcopy = cachedcopy
o.readonly = readonly
o.opened = true
return o.cachedcopy, nil
}
return nil, fmt.Errorf("fetch error retry cnt reached: obj=%s tfile=%v errs:[%v]", o.name, o.cachepath, errs)
}
// File get the current file handle for cached copy.
func (o *object) File() *os.File {
return o.cachedcopy
}
// Read bytes from underlying/cached file
func (o *object) Read(p []byte) (n int, err error) {
return o.cachedcopy.Read(p)
}
// Write bytes to local file, will be synced on close/sync.
func (o *object) Write(p []byte) (n int, err error) {
if o.cachedcopy == nil {
_, err := o.Open(cloudstorage.ReadWrite)
if err != nil {
return 0, err
}
}
return o.cachedcopy.Write(p)
}
// Sync syncs any changes in file up to s3.
func (o *object) Sync() error {
if !o.opened {
return fmt.Errorf("object isn't opened object:%s", o.name)
}
if o.readonly {
return fmt.Errorf("trying to Sync a readonly object:%s", o.name)
}
cachedcopy, err := os.OpenFile(o.cachepath, os.O_RDWR, 0664)
if err != nil {
return fmt.Errorf("couldn't open localfile for sync'ing. local=%s err=%v", o.cachepath, err)
}
defer cachedcopy.Close()
// Create an uploader with the session and default options
uploader := s3manager.NewUploader(o.fs.sess)
if _, err := cachedcopy.Seek(0, os.SEEK_SET); err != nil {
return fmt.Errorf("error seeking to start of cachedcopy err=%v", err) //don't retry on local filesystem errors
}
// Upload the file to S3.
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(o.fs.bucket),
Key: aws.String(o.name),
Body: cachedcopy,
})
if err != nil {
gou.Warnf("could not upload %v", err)
return fmt.Errorf("failed to upload file, %v", err)
}
return nil
}
// Close this object
func (o *object) Close() error {
if !o.opened {
return nil
}
defer func() {
os.Remove(o.cachepath)
o.cachedcopy = nil
o.opened = false
}()
if !o.readonly {
err := o.cachedcopy.Sync()
if err != nil {
return err
}
}
err := o.cachedcopy.Close()
if err != nil {
if !strings.Contains(err.Error(), os.ErrClosed.Error()) {
return err
}
}
if o.opened && !o.readonly {
err := o.Sync()
if err != nil {
gou.Errorf("error on sync %v err=%v", o.cachepath, err)
return err
}
}
return nil
}
// Release this object, cleanup cached copy.
func (o *object) Release() error {
if o.cachedcopy != nil {
gou.Infof("release %q vs %q", o.cachedcopy.Name(), o.cachepath)
o.cachedcopy.Close()
return os.Remove(o.cachepath)
}
os.Remove(o.cachepath)
return nil
}