This repository has been archived by the owner on Oct 15, 2021. It is now read-only.
generated from beyondstorage/go-service-example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
540 lines (460 loc) · 15.7 KB
/
utils.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
package s3
import (
"context"
"crypto/md5"
"encoding/base64"
"errors"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
signerv4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/aws/smithy-go"
"github.com/aws/smithy-go/middleware"
"github.com/beyondstorage/go-endpoint"
ps "github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/pkg/credential"
"github.com/beyondstorage/go-storage/v4/pkg/httpclient"
"github.com/beyondstorage/go-storage/v4/services"
typ "github.com/beyondstorage/go-storage/v4/types"
)
// Service is the s3 service config.
type Service struct {
cfg *aws.Config
service *s3.Client
defaultPairs DefaultServicePairs
features ServiceFeatures
typ.UnimplementedServicer
}
// String implements Servicer.String
func (s *Service) String() string {
return fmt.Sprintf("Servicer s3")
}
// Storage is the s3 object storage service.
type Storage struct {
service *s3.Client
name string
workDir string
defaultPairs DefaultStoragePairs
features StorageFeatures
typ.UnimplementedStorager
typ.UnimplementedDirer
typ.UnimplementedMultiparter
typ.UnimplementedLinker
typ.UnimplementedStorageHTTPSigner
typ.UnimplementedMultipartHTTPSigner
}
// String implements Storager.String
func (s *Storage) String() string {
return fmt.Sprintf(
"Storager s3 {Name: %s, WorkDir: %s}",
s.name, s.workDir,
)
}
// New will create both Servicer and Storager.
func New(pairs ...typ.Pair) (typ.Servicer, typ.Storager, error) {
return newServicerAndStorager(pairs...)
}
// NewServicer will create Servicer only.
func NewServicer(pairs ...typ.Pair) (typ.Servicer, error) {
return newServicer(pairs...)
}
// NewStorager will create Storager only.
func NewStorager(pairs ...typ.Pair) (typ.Storager, error) {
_, store, err := newServicerAndStorager(pairs...)
return store, err
}
func newServicer(pairs ...typ.Pair) (srv *Service, err error) {
defer func() {
if err != nil {
err = services.InitError{Op: "new_servicer", Type: Type, Err: formatError(err), Pairs: pairs}
}
}()
opt, err := parsePairServiceNew(pairs)
if err != nil {
return nil, err
}
//Set s3 config's endpoint
customResolver := aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) {
if opt.HasEndpoint {
ep, err := endpoint.Parse(opt.Endpoint)
if err != nil {
return aws.Endpoint{}, err
}
var url string
switch ep.Protocol() {
case endpoint.ProtocolHTTP:
url, _, _ = ep.HTTP()
case endpoint.ProtocolHTTPS:
url, _, _ = ep.HTTPS()
default:
return aws.Endpoint{}, services.PairUnsupportedError{Pair: ps.WithEndpoint(opt.Endpoint)}
}
return aws.Endpoint{
URL: url,
}, nil
}
// returning EndpointNotFoundError will allow the service to fallback to it's default resolution
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithEndpointResolver(customResolver))
if err != nil {
return nil, err
}
// Set s3 config's http client
cfg.HTTPClient = httpclient.New(opt.HTTPClientOptions)
cp, err := credential.Parse(opt.Credential)
if err != nil {
return nil, err
}
switch cp.Protocol() {
case credential.ProtocolHmac:
ak, sk := cp.Hmac()
cfg.Credentials = aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(ak, sk, ""))
default:
return nil, services.PairUnsupportedError{Pair: ps.WithCredential(opt.Credential)}
}
srv = &Service{
cfg: &cfg,
service: newS3Service(&cfg),
}
if opt.HasDefaultServicePairs {
srv.defaultPairs = opt.DefaultServicePairs
}
if opt.HasServiceFeatures {
srv.features = opt.ServiceFeatures
}
return
}
// New will create a new s3 service.
func newServicerAndStorager(pairs ...typ.Pair) (srv *Service, store *Storage, err error) {
srv, err = newServicer(pairs...)
if err != nil {
return
}
store, err = srv.newStorage(pairs...)
if err != nil {
err = services.InitError{Op: "new_storager", Type: Type, Err: formatError(err), Pairs: pairs}
return
}
return
}
// All available storage classes are listed here.
const (
StorageClassStandard = s3types.ObjectStorageClassStandard
StorageClassReducedRedundancy = s3types.ObjectStorageClassReducedRedundancy
StorageClassGlacier = s3types.ObjectStorageClassGlacier
StorageClassStandardIa = s3types.ObjectStorageClassStandardIa
StorageClassOnezoneIa = s3types.ObjectStorageClassOnezoneIa
StorageClassIntelligentTiering = s3types.ObjectStorageClassIntelligentTiering
StorageClassDeepArchive = s3types.ObjectStorageClassDeepArchive
)
func formatError(err error) error {
if _, ok := err.(services.InternalError); ok {
return err
}
e := &smithy.GenericAPIError{}
if ok := errors.As(err, &e); !ok {
return fmt.Errorf("%w: %v", services.ErrUnexpected, err)
}
switch e.Code {
// AWS SDK will use status code to generate awserr.Error, so "NotFound" should also be supported.
case "NoSuchKey", "NotFound":
return fmt.Errorf("%w: %v", services.ErrObjectNotExist, err)
case "AccessDenied":
return fmt.Errorf("%w: %v", services.ErrPermissionDenied, err)
default:
return fmt.Errorf("%w: %v", services.ErrUnexpected, err)
}
}
func newS3Service(cfgs *aws.Config) (srv *s3.Client) {
// S3 will calculate payload's content-sha256 by default, we change this behavior for following reasons:
// - To support uploading content without seek support: stdin, bytes.Reader
// - To allow user decide when to calculate the hash, especially for big files
srv = s3.NewFromConfig(*cfgs, func(options *s3.Options) {
options.APIOptions = append(options.APIOptions,
func(stack *middleware.Stack) error {
// With removing PayloadSHA256 and adding UnsignedPayload, signer will set "X-Amz-Content-Sha256" to "UNSIGNED-PAYLOAD"
signerv4.RemoveComputePayloadSHA256Middleware(stack)
signerv4.AddUnsignedPayloadMiddleware(stack)
signerv4.RemoveContentSHA256HeaderMiddleware(stack)
return signerv4.AddContentSHA256HeaderMiddleware(stack)
})
})
return
}
// newStorage will create a new client.
func (s *Service) newStorage(pairs ...typ.Pair) (st *Storage, err error) {
optStorage, err := parsePairStorageNew(pairs)
if err != nil {
return nil, err
}
if optStorage.HasLocation {
s.cfg.Region = optStorage.Location
}
st = &Storage{
service: newS3Service(s.cfg),
name: optStorage.Name,
workDir: "/",
}
if optStorage.HasDefaultStoragePairs {
st.defaultPairs = optStorage.DefaultStoragePairs
}
if optStorage.HasStorageFeatures {
st.features = optStorage.StorageFeatures
}
if optStorage.HasWorkDir {
st.workDir = optStorage.WorkDir
}
return st, nil
}
func (s *Service) formatError(op string, err error, name string) error {
if err == nil {
return nil
}
return services.ServiceError{
Op: op,
Err: formatError(err),
Servicer: s,
Name: name,
}
}
// getAbsPath will calculate object storage's abs path
func (s *Storage) getAbsPath(path string) string {
prefix := strings.TrimPrefix(s.workDir, "/")
return prefix + path
}
// getRelPath will get object storage's rel path.
func (s *Storage) getRelPath(path string) string {
prefix := strings.TrimPrefix(s.workDir, "/")
return strings.TrimPrefix(path, prefix)
}
func (s *Storage) formatError(op string, err error, path ...string) error {
if err == nil {
return nil
}
return services.StorageError{
Op: op,
Err: formatError(err),
Storager: s,
Path: path,
}
}
func (s *Storage) formatFileObject(v s3types.Object) (o *typ.Object, err error) {
o = s.newObject(false)
o.ID = *v.Key
o.Path = s.getRelPath(*v.Key)
// If you have enabled virtual link, you will not get the accurate object type.
// If you want to get the exact object mode, please use `stat`
o.Mode |= typ.ModeRead
o.SetContentLength(v.Size)
o.SetLastModified(aws.ToTime(v.LastModified))
if v.ETag != nil {
o.SetEtag(*v.ETag)
}
var sm ObjectSystemMetadata
//v.StorageClass's type is s3types.ObjectStorageClass, which is equivalent to string
sm.StorageClass = string(v.StorageClass)
o.SetSystemMetadata(sm)
return
}
func (s *Storage) newObject(done bool) *typ.Object {
return typ.NewObject(s, done)
}
// All available server side algorithm are listed here.
const (
ServerSideEncryptionAes256 = s3types.ServerSideEncryptionAes256
ServerSideEncryptionAwsKms = s3types.ServerSideEncryptionAwsKms
)
func calculateEncryptionHeaders(algo string, key []byte) (algorithm, keyBase64, keyMD5Base64 *string, err error) {
if len(key) != 32 {
err = ErrServerSideEncryptionCustomerKeyInvalid
return
}
kB64 := base64.StdEncoding.EncodeToString(key)
kMD5 := md5.Sum(key)
kMD5B64 := base64.StdEncoding.EncodeToString(kMD5[:])
return &algo, &kB64, &kMD5B64, nil
}
// multipartXXX are multipart upload restriction in S3, see more details at:
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/qfacts.html
const (
// multipartNumberMaximum is the max part count supported.
multipartNumberMaximum = 10000
// multipartSizeMaximum is the maximum size for each part, 5GB.
multipartSizeMaximum = 5 * 1024 * 1024 * 1024
// multipartSizeMinimum is the minimum size for each part, 5MB.
multipartSizeMinimum = 5 * 1024 * 1024
)
const (
// writeSizeMaximum is the maximum size for each object with a single PUT operation, 5GB.
// ref: https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html
writeSizeMaximum = 5 * 1024 * 1024 * 1024
)
func (s *Storage) formatGetObjectInput(path string, opt pairStorageRead) (input *s3.GetObjectInput, err error) {
rp := s.getAbsPath(path)
input = &s3.GetObjectInput{
Bucket: aws.String(s.name),
Key: aws.String(rp),
}
if opt.HasOffset && opt.HasSize {
input.Range = aws.String(fmt.Sprintf("bytes=%d-%d", opt.Offset, opt.Offset+opt.Size-1))
} else if opt.HasOffset && !opt.HasSize {
input.Range = aws.String(fmt.Sprintf("bytes=%d-", opt.Offset))
} else if !opt.HasOffset && opt.HasSize {
input.Range = aws.String(fmt.Sprintf("bytes=0-%d", opt.Size-1))
}
if opt.HasExceptedBucketOwner {
input.ExpectedBucketOwner = &opt.ExceptedBucketOwner
}
if opt.HasServerSideEncryptionCustomerAlgorithm {
input.SSECustomerAlgorithm, input.SSECustomerKey, input.SSECustomerKeyMD5, err = calculateEncryptionHeaders(opt.ServerSideEncryptionCustomerAlgorithm, opt.ServerSideEncryptionCustomerKey)
if err != nil {
return nil, err
}
}
return
}
func (s *Storage) formatPutObjectInput(path string, size int64, opt pairStorageWrite) (input *s3.PutObjectInput, err error) {
rp := s.getAbsPath(path)
input = &s3.PutObjectInput{
Bucket: aws.String(s.name),
Key: aws.String(rp),
ContentLength: size,
}
if opt.HasContentMd5 {
input.ContentMD5 = &opt.ContentMd5
}
if opt.HasStorageClass {
input.StorageClass = s3types.StorageClass(opt.StorageClass)
}
if opt.HasExceptedBucketOwner {
input.ExpectedBucketOwner = &opt.ExceptedBucketOwner
}
if opt.HasServerSideEncryptionBucketKeyEnabled {
input.BucketKeyEnabled = opt.ServerSideEncryptionBucketKeyEnabled
}
if opt.HasServerSideEncryptionCustomerAlgorithm {
input.SSECustomerAlgorithm, input.SSECustomerKey, input.SSECustomerKeyMD5, err = calculateEncryptionHeaders(opt.ServerSideEncryptionCustomerAlgorithm, opt.ServerSideEncryptionCustomerKey)
if err != nil {
return nil, err
}
}
if opt.HasServerSideEncryptionAwsKmsKeyID {
input.SSEKMSKeyId = &opt.ServerSideEncryptionAwsKmsKeyID
}
if opt.HasServerSideEncryptionContext {
encodedKMSEncryptionContext := base64.StdEncoding.EncodeToString([]byte(opt.ServerSideEncryptionContext))
input.SSEKMSEncryptionContext = &encodedKMSEncryptionContext
}
if opt.HasServerSideEncryption {
input.ServerSideEncryption = s3types.ServerSideEncryption(opt.ServerSideEncryption)
}
return
}
func (s *Storage) formatAbortMultipartUploadInput(path string, opt pairStorageDelete) (input *s3.AbortMultipartUploadInput) {
rp := s.getAbsPath(path)
input = &s3.AbortMultipartUploadInput{
Bucket: aws.String(s.name),
Key: aws.String(rp),
UploadId: aws.String(opt.MultipartID),
}
if opt.HasExceptedBucketOwner {
input.ExpectedBucketOwner = &opt.ExceptedBucketOwner
}
return
}
func (s *Storage) formatDeleteObjectInput(path string, opt pairStorageDelete) (input *s3.DeleteObjectInput, err error) {
rp := s.getAbsPath(path)
if opt.HasObjectMode && opt.ObjectMode.IsDir() {
if !s.features.VirtualDir {
err = services.PairUnsupportedError{Pair: ps.WithObjectMode(opt.ObjectMode)}
return nil, err
}
rp += "/"
}
input = &s3.DeleteObjectInput{
Bucket: aws.String(s.name),
Key: aws.String(rp),
}
if opt.HasExceptedBucketOwner {
input.ExpectedBucketOwner = &opt.ExceptedBucketOwner
}
return
}
func (s *Storage) formatCreateMultipartUploadInput(path string, opt pairStorageCreateMultipart) (input *s3.CreateMultipartUploadInput, err error) {
rp := s.getAbsPath(path)
input = &s3.CreateMultipartUploadInput{
Bucket: aws.String(s.name),
Key: aws.String(rp),
}
if opt.HasServerSideEncryptionBucketKeyEnabled {
input.BucketKeyEnabled = opt.ServerSideEncryptionBucketKeyEnabled
}
if opt.HasExceptedBucketOwner {
input.ExpectedBucketOwner = &opt.ExceptedBucketOwner
}
if opt.HasServerSideEncryptionCustomerAlgorithm {
input.SSECustomerAlgorithm, input.SSECustomerKey, input.SSECustomerKeyMD5, err = calculateEncryptionHeaders(opt.ServerSideEncryptionCustomerAlgorithm, opt.ServerSideEncryptionCustomerKey)
if err != nil {
return nil, err
}
}
if opt.HasServerSideEncryptionAwsKmsKeyID {
input.SSEKMSKeyId = &opt.ServerSideEncryptionAwsKmsKeyID
}
if opt.HasServerSideEncryptionContext {
encodedKMSEncryptionContext := base64.StdEncoding.EncodeToString([]byte(opt.ServerSideEncryptionContext))
input.SSEKMSEncryptionContext = &encodedKMSEncryptionContext
}
if opt.HasServerSideEncryption {
input.ServerSideEncryption = s3types.ServerSideEncryption(opt.ServerSideEncryption)
}
return
}
func (s *Storage) formatCompleteMultipartUploadInput(o *typ.Object, parts []*typ.Part, opt pairStorageCompleteMultipart) (input *s3.CompleteMultipartUploadInput) {
upload := &s3types.CompletedMultipartUpload{}
for _, v := range parts {
upload.Parts = append(upload.Parts, s3types.CompletedPart{
ETag: aws.String(v.ETag),
// For users the `PartNumber` is zero-based. But for S3, the effective `PartNumber` is [1, 10000].
// Set PartNumber=v.Index+1 here to ensure pass in an effective `PartNumber` for `CompletedPart`.
PartNumber: int32(v.Index + 1),
})
}
input = &s3.CompleteMultipartUploadInput{
Bucket: aws.String(s.name),
Key: aws.String(o.ID),
MultipartUpload: upload,
UploadId: aws.String(o.MustGetMultipartID()),
}
if opt.HasExceptedBucketOwner {
input.ExpectedBucketOwner = &opt.ExceptedBucketOwner
}
return
}
func (s *Storage) formatUploadPartInput(o *typ.Object, size int64, index int, opt pairStorageWriteMultipart) (input *s3.UploadPartInput, err error) {
input = &s3.UploadPartInput{
Bucket: &s.name,
// For S3, the `PartNumber` is [1, 10000]. But for users, the `PartNumber` is zero-based.
// Set PartNumber=index+1 here to ensure pass in an effective `PartNumber` for `UploadPart`.
// ref: https://docs.aws.amazon.com/AmazonS3/latest/userguide/mpuoverview.html
PartNumber: int32(index + 1),
Key: aws.String(o.ID),
UploadId: aws.String(o.MustGetMultipartID()),
ContentLength: size,
}
if opt.HasExceptedBucketOwner {
input.ExpectedBucketOwner = &opt.ExceptedBucketOwner
}
if opt.HasServerSideEncryptionCustomerAlgorithm {
input.SSECustomerAlgorithm, input.SSECustomerKey, input.SSECustomerKeyMD5, err = calculateEncryptionHeaders(opt.ServerSideEncryptionCustomerAlgorithm, opt.ServerSideEncryptionCustomerKey)
if err != nil {
return nil, err
}
}
return
}