/
s3_versioned.go
110 lines (85 loc) · 2.82 KB
/
s3_versioned.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
package s3
import (
"context"
"strings"
"github.com/minio/minio-go/v7"
"github.com/pkg/errors"
"github.com/kopia/kopia/repo/blob"
)
// versionMetadata has metadata for a single BLOB version.
type versionMetadata struct {
blob.Metadata
// Versioning related information
IsLatest bool
IsDeleteMarker bool
Version string
}
// versionMetadataCallback is called when processing the metadata for each blob version.
type versionMetadataCallback func(versionMetadata) error
// IsVersioned returns whether versioning is enabled in the store.
// It returns true even if versioning is enabled but currently suspended for the
// bucket. Notice that when object locking is enabled in a bucket, object
// versioning is enabled and cannot be suspended.
func (s *s3Storage) IsVersioned(ctx context.Context) (bool, error) {
vi, err := s.cli.GetBucketVersioning(ctx, s.BucketName)
if err != nil {
return false, errors.Wrapf(err, "could not get versioning info for %s", s.BucketName)
}
return vi.Enabled(), nil
}
// getBlobVersions lists all the versions for the blob with the given ID.
func (s *s3Storage) getBlobVersions(ctx context.Context, prefix blob.ID, callback versionMetadataCallback) error {
var foundBlobs bool
if err := s.list(ctx, prefix, true, func(vm versionMetadata) error {
foundBlobs = true
return callback(vm)
}); err != nil {
return err
}
if !foundBlobs {
return blob.ErrBlobNotFound
}
return nil
}
// listBlobVersions lists all versions for all the blobs with the given blob ID prefix.
func (s *s3Storage) listBlobVersions(ctx context.Context, prefix blob.ID, callback versionMetadataCallback) error {
return s.list(ctx, prefix, false, callback)
}
func (s *s3Storage) list(ctx context.Context, prefix blob.ID, onlyMatching bool, callback versionMetadataCallback) error {
opts := minio.ListObjectsOptions{
Prefix: s.getObjectNameString(prefix),
Recursive: !onlyMatching,
WithVersions: true,
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
for o := range s.cli.ListObjects(ctx, s.BucketName, opts) {
if o.Err != nil {
return errors.Wrapf(o.Err, "could not list objects with prefix %q", opts.Prefix)
}
if onlyMatching && o.Key != opts.Prefix {
return nil
}
om := infoToVersionMetadata(s.Prefix, &o)
if err := callback(om); err != nil {
return errors.Wrapf(err, "callback failed for %q", o.Key)
}
}
return nil
}
func toBlobID(blobName, prefix string) blob.ID {
return blob.ID(strings.TrimPrefix(blobName, prefix))
}
func infoToVersionMetadata(prefix string, oi *minio.ObjectInfo) versionMetadata {
bm := blob.Metadata{
BlobID: toBlobID(oi.Key, prefix),
Length: oi.Size,
Timestamp: oi.LastModified,
}
return versionMetadata{
Metadata: bm,
IsLatest: oi.IsLatest,
IsDeleteMarker: oi.IsDeleteMarker,
Version: oi.VersionID,
}
}