| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"io" |
| 6 |
"net/url" |
| 7 |
"path" |
| 8 |
|
| 9 |
"cloud.google.com/go/storage" |
| 10 |
"github.com/pkg/errors" |
| 11 |
"github.com/sirupsen/logrus" |
| 12 |
) |
| 13 |
|
| 14 |
// GCIndexStore is a read-write index store with Google Storage backing |
| 15 |
type GCIndexStore struct { |
| 16 |
GCStoreBase |
| 17 |
} |
| 18 |
|
| 19 |
// NewGCIndexStore creates an index store with Google Storage backing. The URL |
| 20 |
// should be provided like this: gc://bucket/prefix |
| 21 |
func NewGCIndexStore(location *url.URL, opt StoreOptions) (s GCIndexStore, e error) { |
| 22 |
if err := opt.ValidateIndexOptions(); err != nil { |
| 23 |
return s, err |
| 24 |
} |
| 25 |
b, err := NewGCStoreBase(location, opt) |
| 26 |
if err != nil { |
| 27 |
return s, err |
| 28 |
} |
| 29 |
return GCIndexStore{b}, nil |
| 30 |
} |
| 31 |
|
| 32 |
// GetIndexReader returns a reader for an index from an Google Storage store. Fails if the specified index |
| 33 |
// file does not exist. |
| 34 |
func (s GCIndexStore) GetIndexReader(name string) (r io.ReadCloser, err error) { |
| 35 |
ctx := context.TODO() |
| 36 |
|
| 37 |
var ( |
| 38 |
log = Log.WithFields(logrus.Fields{ |
| 39 |
"bucket": s.bucket, |
| 40 |
"name": s.prefix + name, |
| 41 |
}) |
| 42 |
) |
| 43 |
|
| 44 |
obj, err := s.client.Object(s.prefix + name).NewReader(ctx) |
| 45 |
|
| 46 |
if err == storage.ErrObjectNotExist { |
| 47 |
log.Warning("Unable to create reader for object in GCS bucket; the object may not exist, or the bucket may not exist, or you may not have permission to access it") |
| 48 |
return nil, errors.Wrap(err, s.String()) |
| 49 |
} else if err != nil { |
| 50 |
log.WithError(err).Error("Error when creating index reader from GCS bucket") |
| 51 |
return nil, errors.Wrap(err, s.String()) |
| 52 |
} |
| 53 |
|
| 54 |
log.Debug("Created index reader from GCS bucket") |
| 55 |
return obj, nil |
| 56 |
} |
| 57 |
|
| 58 |
// GetIndex returns an Index structure from the store |
| 59 |
func (s GCIndexStore) GetIndex(name string) (i Index, e error) { |
| 60 |
obj, err := s.GetIndexReader(name) |
| 61 |
if err != nil { |
| 62 |
return i, err |
| 63 |
} |
| 64 |
defer obj.Close() |
| 65 |
return IndexFromReader(obj) |
| 66 |
} |
| 67 |
|
| 68 |
// StoreIndex writes the index file to the Google Storage store |
| 69 |
func (s GCIndexStore) StoreIndex(name string, idx Index) error { |
| 70 |
ctx := context.TODO() |
| 71 |
|
| 72 |
var ( |
| 73 |
log = Log.WithFields(logrus.Fields{ |
| 74 |
"bucket": s.bucket, |
| 75 |
"name": s.prefix + name, |
| 76 |
}) |
| 77 |
) |
| 78 |
|
| 79 |
w := s.client.Object(s.prefix + name).NewWriter(ctx) |
| 80 |
w.ContentType = "application/octet-stream" |
| 81 |
|
| 82 |
_, err := idx.WriteTo(w) |
| 83 |
|
| 84 |
if err != nil { |
| 85 |
log.WithError(err).Error("Error when copying data from local filesystem to object in GCS bucket") |
| 86 |
w.Close() |
| 87 |
return errors.Wrap(err, path.Base(s.Location)) |
| 88 |
} |
| 89 |
|
| 90 |
err = w.Close() |
| 91 |
|
| 92 |
if err != nil { |
| 93 |
log.WithError(err).Error("Error when finalizing copying of data from local filesystem to object in GCS bucket") |
| 94 |
return errors.Wrap(err, path.Base(s.Location)) |
| 95 |
} |
| 96 |
|
| 97 |
log.Debug("Index written to GCS bucket") |
| 98 |
return nil |
| 99 |
} |