-
Notifications
You must be signed in to change notification settings - Fork 340
/
s3_storage.go
348 lines (274 loc) · 8.79 KB
/
s3_storage.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
// Package s3 implements Storage based on an S3 bucket.
package s3
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net/http"
"strings"
"sync/atomic"
"time"
"github.com/efarrer/iothrottler"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/gather"
"github.com/kopia/kopia/internal/iocopy"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/blob/retrying"
)
const (
s3storageType = "s3"
latestVersionID = ""
)
type s3Storage struct {
sendMD5 int32
Options
cli *minio.Client
downloadThrottler *iothrottler.IOThrottlerPool
uploadThrottler *iothrottler.IOThrottlerPool
storageConfig *StorageConfig
}
func (s *s3Storage) GetBlob(ctx context.Context, b blob.ID, offset, length int64, output *gather.WriteBuffer) error {
return s.getBlobWithVersion(ctx, b, latestVersionID, offset, length, output)
}
// getBlobWithVersion returns full or partial contents of a blob with given ID and version.
func (s *s3Storage) getBlobWithVersion(ctx context.Context, b blob.ID, version string, offset, length int64, output *gather.WriteBuffer) error {
output.Reset()
attempt := func() error {
opt := minio.GetObjectOptions{VersionID: version}
if length > 0 {
if err := opt.SetRange(offset, offset+length-1); err != nil {
return errors.Wrap(blob.ErrInvalidRange, "unable to set range")
}
}
if length == 0 {
// zero-length ranges require special handling, set non-zero range and
// we won't be trying to read the response anyway.
if err := opt.SetRange(0, 1); err != nil {
return errors.Wrap(blob.ErrInvalidRange, "unable to set range")
}
}
o, err := s.cli.GetObject(ctx, s.BucketName, s.getObjectNameString(b), opt)
if err != nil {
return errors.Wrap(err, "GetObject")
}
defer o.Close() //nolint:errcheck
throttled, err := s.downloadThrottler.AddReader(o)
if err != nil {
return errors.Wrap(err, "AddReader")
}
if length == 0 {
return nil
}
// nolint:wrapcheck
return iocopy.JustCopy(output, throttled)
}
if err := attempt(); err != nil {
return translateError(err)
}
// nolint:wrapcheck
return blob.EnsureLengthExactly(output.Length(), length)
}
func translateError(err error) error {
var me minio.ErrorResponse
if errors.As(err, &me) {
switch me.StatusCode {
case http.StatusOK:
return nil
case http.StatusNotFound:
return blob.ErrBlobNotFound
case http.StatusRequestedRangeNotSatisfiable:
return blob.ErrInvalidRange
}
}
return err
}
func (s *s3Storage) GetMetadata(ctx context.Context, b blob.ID) (blob.Metadata, error) {
vm, err := s.getVersionMetadata(ctx, b, "")
return vm.Metadata, err
}
func (s *s3Storage) getVersionMetadata(ctx context.Context, b blob.ID, version string) (versionMetadata, error) {
opts := minio.GetObjectOptions{
VersionID: version,
}
oi, err := s.cli.StatObject(ctx, s.BucketName, s.getObjectNameString(b), opts)
if err != nil {
return versionMetadata{}, errors.Wrap(translateError(err), "StatObject")
}
return infoToVersionMetadata(s.Prefix, &oi), nil
}
func (s *s3Storage) PutBlob(ctx context.Context, b blob.ID, data blob.Bytes) error {
_, err := s.putBlob(ctx, b, data)
return err
}
func (s *s3Storage) putBlob(ctx context.Context, b blob.ID, data blob.Bytes) (versionMetadata, error) {
throttled, err := s.uploadThrottler.AddReader(io.NopCloser(data.Reader()))
if err != nil {
return versionMetadata{}, errors.Wrap(err, "AddReader")
}
storageClass := s.storageConfig.getStorageClassForBlobID(b)
uploadInfo, err := s.cli.PutObject(ctx, s.BucketName, s.getObjectNameString(b), throttled, int64(data.Length()), minio.PutObjectOptions{
ContentType: "application/x-kopia",
SendContentMd5: atomic.LoadInt32(&s.sendMD5) > 0,
StorageClass: storageClass,
})
var er minio.ErrorResponse
if errors.As(err, &er) && er.Code == "InvalidRequest" && strings.Contains(strings.ToLower(er.Message), "content-md5") {
atomic.StoreInt32(&s.sendMD5, 1) // set sendMD5 on retry
return versionMetadata{}, err // nolint:wrapcheck
}
if errors.Is(err, io.EOF) && uploadInfo.Size == 0 {
// special case empty stream
_, err = s.cli.PutObject(ctx, s.BucketName, s.getObjectNameString(b), bytes.NewBuffer(nil), 0, minio.PutObjectOptions{
ContentType: "application/x-kopia",
StorageClass: storageClass,
})
}
if err != nil {
return versionMetadata{}, err // nolint:wrapcheck
}
return versionMetadata{
Metadata: blob.Metadata{
BlobID: b,
Length: uploadInfo.Size,
Timestamp: uploadInfo.LastModified,
},
Version: uploadInfo.VersionID,
}, nil
}
func (s *s3Storage) SetTime(ctx context.Context, b blob.ID, t time.Time) error {
return blob.ErrSetTimeUnsupported
}
func (s *s3Storage) DeleteBlob(ctx context.Context, b blob.ID) error {
err := translateError(s.cli.RemoveObject(ctx, s.BucketName, s.getObjectNameString(b), minio.RemoveObjectOptions{}))
if errors.Is(err, blob.ErrBlobNotFound) {
return nil
}
return err
}
func (s *s3Storage) getObjectNameString(b blob.ID) string {
return s.Prefix + string(b)
}
func (s *s3Storage) ListBlobs(ctx context.Context, prefix blob.ID, callback func(blob.Metadata) error) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
oi := s.cli.ListObjects(ctx, s.BucketName, minio.ListObjectsOptions{
Prefix: s.getObjectNameString(prefix),
})
for o := range oi {
if err := o.Err; err != nil {
return err
}
bm := blob.Metadata{
BlobID: blob.ID(o.Key[len(s.Prefix):]),
Length: o.Size,
Timestamp: o.LastModified,
}
if bm.BlobID == ConfigName {
continue
}
if err := callback(bm); err != nil {
return err
}
}
return nil
}
func (s *s3Storage) ConnectionInfo() blob.ConnectionInfo {
return blob.ConnectionInfo{
Type: s3storageType,
Config: &s.Options,
}
}
func (s *s3Storage) Close(ctx context.Context) error {
return nil
}
func (s *s3Storage) String() string {
return fmt.Sprintf("s3://%v/%v", s.BucketName, s.Prefix)
}
func (s *s3Storage) DisplayName() string {
return fmt.Sprintf("S3: %v %v", s.Endpoint, s.BucketName)
}
func (s *s3Storage) FlushCaches(ctx context.Context) error {
return nil
}
func toBandwidth(bytesPerSecond int) iothrottler.Bandwidth {
if bytesPerSecond <= 0 {
return iothrottler.Unlimited
}
return iothrottler.Bandwidth(bytesPerSecond) * iothrottler.BytesPerSecond
}
func getCustomTransport(insecureSkipVerify bool) (transport *http.Transport) {
// nolint:gosec
customTransport := &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: insecureSkipVerify}}
return customTransport
}
// New creates new S3-backed storage with specified options:
//
// - the 'BucketName' field is required and all other parameters are optional.
func New(ctx context.Context, opt *Options) (blob.Storage, error) {
st, err := newStorage(ctx, opt)
if err != nil {
return nil, err
}
s, err := maybePointInTimeStore(ctx, st, opt.PointInTime)
if err != nil {
return nil, err
}
return retrying.NewWrapper(s), nil
}
func newStorage(ctx context.Context, opt *Options) (*s3Storage, error) {
if opt.BucketName == "" {
return nil, errors.New("bucket name must be specified")
}
minioOpts := &minio.Options{
Creds: credentials.NewStaticV4(opt.AccessKeyID, opt.SecretAccessKey, opt.SessionToken),
Secure: !opt.DoNotUseTLS,
Region: opt.Region,
}
if opt.DoNotVerifyTLS {
minioOpts.Transport = getCustomTransport(true)
}
cli, err := minio.New(opt.Endpoint, minioOpts)
if err != nil {
return nil, errors.Wrap(err, "unable to create client")
}
downloadThrottler := iothrottler.NewIOThrottlerPool(toBandwidth(opt.MaxDownloadSpeedBytesPerSecond))
uploadThrottler := iothrottler.NewIOThrottlerPool(toBandwidth(opt.MaxUploadSpeedBytesPerSecond))
ok, err := cli.BucketExists(ctx, opt.BucketName)
if err != nil {
return nil, errors.Wrapf(err, "unable to determine if bucket %q exists", opt.BucketName)
}
if !ok {
return nil, errors.Errorf("bucket %q does not exist", opt.BucketName)
}
s := s3Storage{
Options: *opt,
cli: cli,
sendMD5: 0,
downloadThrottler: downloadThrottler,
uploadThrottler: uploadThrottler,
storageConfig: &StorageConfig{},
}
var scOutput gather.WriteBuffer
if getBlobErr := s.GetBlob(ctx, ConfigName, 0, -1, &scOutput); getBlobErr == nil {
if scErr := s.storageConfig.Load(scOutput.Bytes().Reader()); scErr != nil {
return nil, errors.Wrapf(scErr, "error parsing storage config for bucket %q", opt.BucketName)
}
} else if !errors.Is(getBlobErr, blob.ErrBlobNotFound) {
return nil, errors.Wrapf(getBlobErr, "error retrieving storage config from bucket %q", opt.BucketName)
}
return &s, nil
}
func init() {
blob.AddSupportedStorage(
s3storageType,
func() interface{} {
return &Options{}
},
func(ctx context.Context, o interface{}) (blob.Storage, error) {
return New(ctx, o.(*Options))
})
}