Debian Codegraph @

s3index.go

raw
1 package desync
2
3 import (
4 "io"
5
6 "path"
7
8 "net/url"
9
10 "github.com/minio/minio-go/v6"
11 "github.com/minio/minio-go/v6/pkg/credentials"
12 "github.com/pkg/errors"
13 )
14
15 // S3IndexStore is a read-write index store with S3 backing
16 type S3IndexStore struct {
17 S3StoreBase
18 }
19
20 // NewS3IndexStore creates an index store with S3 backing. The URL
21 // should be provided like this: s3+http://host:port/bucket
22 // Credentials are passed in via the environment variables S3_ACCESS_KEY
23 // and S3_SECRET_KEY, or via the desync config file.
24 func NewS3IndexStore(location *url.URL, s3Creds *credentials.Credentials, region string, opt StoreOptions, lookupType minio.BucketLookupType) (s S3IndexStore, e error) {
25 if err := opt.ValidateIndexOptions(); err != nil {
26 return s, err
27 }
28 b, err := NewS3StoreBase(location, s3Creds, region, opt, lookupType)
29 if err != nil {
30 return s, err
31 }
32 return S3IndexStore{b}, nil
33 }
34
35 // GetIndexReader returns a reader for an index from an S3 store. Fails if the specified index
36 // file does not exist.
37 func (s S3IndexStore) GetIndexReader(name string) (r io.ReadCloser, e error) {
38 obj, err := s.client.GetObject(s.bucket, s.prefix+name, minio.GetObjectOptions{})
39 if err != nil {
40 return r, errors.Wrap(err, s.String())
41 }
42 return obj, nil
43 }
44
45 // GetIndex returns an Index structure from the store
46 func (s S3IndexStore) GetIndex(name string) (i Index, e error) {
47 obj, err := s.GetIndexReader(name)
48 if err != nil {
49 return i, err
50 }
51 defer obj.Close()
52 return IndexFromReader(obj)
53 }
54
55 // StoreIndex writes the index file to the S3 store
56 func (s S3IndexStore) StoreIndex(name string, idx Index) error {
57 contentType := "application/octet-stream"
58 r, w := io.Pipe()
59
60 go func() {
61 defer w.Close()
62 idx.WriteTo(w)
63 }()
64
65 _, err := s.client.PutObject(s.bucket, s.prefix+name, r, -1, minio.PutObjectOptions{ContentType: contentType})
66 return errors.Wrap(err, path.Base(s.Location))
67 }