generated from beyondstorage/go-service-example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
storage.go
820 lines (684 loc) · 21 KB
/
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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
package qingstor
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"time"
"github.com/pengsrc/go-shared/convert"
"github.com/qingstor/qingstor-sdk-go/v4/service"
ps "github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/pkg/iowrap"
"github.com/beyondstorage/go-storage/v4/services"
. "github.com/beyondstorage/go-storage/v4/types"
)
func (s *Storage) commitAppend(ctx context.Context, o *Object, opt pairStorageCommitAppend) (err error) {
return
}
func (s *Storage) completeMultipart(ctx context.Context, o *Object, parts []*Part, opt pairStorageCompleteMultipart) (err error) {
objectParts := make([]*service.ObjectPartType, 0, len(parts))
for _, v := range parts {
objectParts = append(objectParts, &service.ObjectPartType{
Etag: service.String(v.ETag),
PartNumber: service.Int(v.Index),
Size: service.Int64(v.Size),
})
}
_, err = s.bucket.CompleteMultipartUploadWithContext(ctx, o.ID, &service.CompleteMultipartUploadInput{
UploadID: service.String(o.MustGetMultipartID()),
ObjectParts: objectParts,
})
if err != nil {
return
}
o.Mode.Del(ModePart)
o.Mode.Add(ModeRead)
return
}
func (s *Storage) copy(ctx context.Context, src string, dst string, opt pairStorageCopy) (err error) {
rs := s.getAbsPath(src)
rd := s.getAbsPath(dst)
srcPath := "/" + service.StringValue(s.properties.BucketName) + "/" + url.QueryEscape(rs)
input := &service.PutObjectInput{
XQSCopySource: &srcPath,
}
if opt.HasEncryptionCustomerAlgorithm {
input.XQSEncryptionCustomerAlgorithm, input.XQSEncryptionCustomerKey, input.XQSEncryptionCustomerKeyMD5, err = calculateEncryptionHeaders(opt.EncryptionCustomerAlgorithm, opt.EncryptionCustomerKey)
if err != nil {
return
}
}
if opt.HasCopySourceEncryptionCustomerAlgorithm {
input.XQSCopySourceEncryptionCustomerAlgorithm, input.XQSCopySourceEncryptionCustomerKey, input.XQSCopySourceEncryptionCustomerKeyMD5, err = calculateEncryptionHeaders(opt.CopySourceEncryptionCustomerAlgorithm, opt.CopySourceEncryptionCustomerKey)
if err != nil {
return
}
}
_, err = s.bucket.PutObjectWithContext(ctx, rd, input)
if err != nil {
return
}
return nil
}
func (s *Storage) create(path string, opt pairStorageCreate) (o *Object) {
rp := s.getAbsPath(path)
// handle create multipart object separately
// if opt has multipartID, set object done, because we can't stat multipart object in QingStor
if opt.HasMultipartID {
o = s.newObject(true)
o.Mode = ModePart
o.SetMultipartID(opt.MultipartID)
} else {
if opt.HasObjectMode && opt.ObjectMode.IsDir() {
if !s.features.VirtualDir {
return
}
rp += "/"
o = s.newObject(true)
o.Mode = ModeDir
} else {
o = s.newObject(false)
o.Mode = ModeRead
}
}
o.ID = rp
o.Path = path
return o
}
func (s *Storage) createAppend(ctx context.Context, path string, opt pairStorageCreateAppend) (o *Object, err error) {
rp := s.getAbsPath(path)
// We should set offset to 0 whether the object exists or not.
var offset int64 = 0
// If the object exists, we should delete it.
headInput := &service.HeadObjectInput{}
_, err = s.bucket.HeadObjectWithContext(ctx, rp, headInput)
if err == nil {
_, err = s.bucket.DeleteObject(rp)
if err != nil {
return nil, err
}
}
input := &service.AppendObjectInput{
Position: &offset,
}
if opt.HasContentType {
input.ContentType = &opt.ContentType
}
if opt.HasStorageClass {
input.XQSStorageClass = &opt.StorageClass
}
output, err := s.bucket.AppendObjectWithContext(ctx, rp, input)
if err != nil {
return
}
if output == nil || output.XQSNextAppendPosition == nil {
err = ErrAppendNextPositionEmpty
return
} else {
offset = *output.XQSNextAppendPosition
}
o = s.newObject(true)
o.Mode = ModeRead | ModeAppend
o.ID = rp
o.Path = path
o.SetAppendOffset(offset)
// set metadata
if opt.HasContentType {
o.SetContentType(opt.ContentType)
}
var sm ObjectSystemMetadata
if opt.HasStorageClass {
sm.StorageClass = opt.StorageClass
}
return o, nil
}
func (s *Storage) createDir(ctx context.Context, path string, opt pairStorageCreateDir) (o *Object, err error) {
if !s.features.VirtualDir {
err = NewOperationNotImplementedError("create_dir")
return
}
rp := s.getAbsPath(path)
// Add `/` at the end of path to simulate a directory.
// ref: https://docs.qingcloud.com/qingstor/api/object/put.html
rp += "/"
input := &service.PutObjectInput{
ContentLength: service.Int64(0),
}
if opt.HasStorageClass {
input.XQSStorageClass = service.String(opt.StorageClass)
}
_, err = s.bucket.PutObjectWithContext(ctx, rp, input)
if err != nil {
return
}
o = s.newObject(true)
o.Path = path
o.ID = rp
o.Mode = ModeDir
return
}
// metadataLinkTargetHeader is the name of the user-defined metadata name used to store the target.
const metadataLinkTargetHeader = "x-qs-meta-bs-link-target"
func (s *Storage) createLink(ctx context.Context, path string, target string, opt pairStorageCreateLink) (o *Object, err error) {
rt := s.getAbsPath(target)
rp := s.getAbsPath(path)
input := &service.PutObjectInput{
// As qingstor does not support symlink, we can only use user-defined metadata to simulate it.
// ref: https://github.com/beyondstorage/go-service-qingstor/blob/master/rfcs/79-add-virtual-link-support.md
XQSMetaData: &map[string]string{
metadataLinkTargetHeader: rt,
},
}
_, err = s.bucket.PutObjectWithContext(ctx, rp, input)
if err != nil {
return nil, err
}
o = s.newObject(true)
o.ID = rp
o.Path = path
if !s.features.VirtualLink {
// The virtual link is not enabled, so we set the object mode to `ModeRead`.
o.Mode |= ModeRead
} else {
// qingstor does not have an absolute path, so when we call `getAbsPath`, it will remove the prefix `/`.
// To ensure that the path matches the one the user gets, we should re-add `/` here.
o.SetLinkTarget("/" + rt)
o.Mode |= ModeLink
}
return
}
func (s *Storage) createMultipart(ctx context.Context, path string, opt pairStorageCreateMultipart) (o *Object, err error) {
input := &service.InitiateMultipartUploadInput{}
if opt.HasEncryptionCustomerAlgorithm {
input.XQSEncryptionCustomerAlgorithm, input.XQSEncryptionCustomerKey, input.XQSEncryptionCustomerKeyMD5, err = calculateEncryptionHeaders(opt.EncryptionCustomerAlgorithm, opt.EncryptionCustomerKey)
if err != nil {
return
}
}
rp := s.getAbsPath(path)
output, err := s.bucket.InitiateMultipartUploadWithContext(ctx, rp, input)
if err != nil {
return
}
o = s.newObject(true)
o.ID = rp
o.Path = path
o.Mode |= ModePart
o.SetMultipartID(*output.UploadID)
return o, nil
}
func (s *Storage) delete(ctx context.Context, path string, opt pairStorageDelete) (err error) {
rp := s.getAbsPath(path)
if opt.HasMultipartID {
// QingStor AbortMultipartUpload is idempotent, so we don't need to check upload_not_exists error.
//
// References
// - [GSP-46](https://github.com/beyondstorage/specs/blob/master/rfcs/46-idempotent-delete.md)
// - https://docs.qingcloud.com/qingstor/api/object/multipart/abort_multipart_upload.html
_, err = s.bucket.AbortMultipartUploadWithContext(ctx, rp, &service.AbortMultipartUploadInput{
UploadID: service.String(opt.MultipartID),
})
if err != nil {
return
}
return
}
if opt.HasObjectMode && opt.ObjectMode.IsDir() {
if !s.features.VirtualDir {
err = services.PairUnsupportedError{Pair: ps.WithObjectMode(opt.ObjectMode)}
return
}
rp += "/"
}
// QingStor DeleteObject is idempotent, so we don't need to check object_not_exists error.
//
// - [GSP-46](https://github.com/beyondstorage/specs/blob/master/rfcs/46-idempotent-delete.md)
// - https://docs.qingcloud.com/qingstor/api/object/delete
_, err = s.bucket.DeleteObjectWithContext(ctx, rp)
if err != nil {
return
}
return nil
}
func (s *Storage) fetch(ctx context.Context, path string, url string, opt pairStorageFetch) (err error) {
_, err = s.bucket.PutObjectWithContext(ctx, path, &service.PutObjectInput{
XQSFetchSource: service.String(url),
})
return err
}
func (s *Storage) list(ctx context.Context, path string, opt pairStorageList) (oi *ObjectIterator, err error) {
input := &objectPageStatus{
limit: 200,
prefix: s.getAbsPath(path),
}
if !opt.HasListMode {
// Support `ListModePrefix` as the default `ListMode`.
// ref: [GSP-654](https://github.com/beyondstorage/go-storage/blob/master/docs/rfcs/654-unify-list-behavior.md)
opt.ListMode = ListModePrefix
}
var nextFn NextObjectFunc
switch {
case opt.ListMode.IsPart():
nextFn = s.nextPartObjectPageByPrefix
case opt.ListMode.IsDir():
input.delimiter = "/"
nextFn = s.nextObjectPageByDir
case opt.ListMode.IsPrefix():
nextFn = s.nextObjectPageByPrefix
default:
return nil, services.ListModeInvalidError{Actual: opt.ListMode}
}
return NewObjectIterator(ctx, nextFn, input), nil
}
func (s *Storage) listMultipart(ctx context.Context, o *Object, opt pairStorageListMultipart) (pi *PartIterator, err error) {
input := &partPageStatus{
limit: 200,
prefix: o.ID,
uploadID: o.MustGetMultipartID(),
}
return NewPartIterator(ctx, s.nextPartPage, input), nil
}
func (s *Storage) metadata(opt pairStorageMetadata) (meta *StorageMeta) {
meta = NewStorageMeta()
meta.Name = *s.properties.BucketName
meta.WorkDir = s.workDir
meta.SetLocation(*s.properties.Zone)
// set write restriction
meta.SetWriteSizeMaximum(writeSizeMaximum)
// set copy restriction
meta.SetCopySizeMaximum(copySizeMaximum)
// set append restrictions
meta.SetAppendSizeMaximum(appendSizeMaximum)
meta.SetAppendTotalSizeMaximum(appendTotalSizeMaximum)
// set multipart restrictions
meta.SetMultipartNumberMaximum(multipartNumberMaximum)
meta.SetMultipartSizeMaximum(multipartSizeMaximum)
meta.SetMultipartSizeMinimum(multipartSizeMinimum)
return meta
}
func (s *Storage) move(ctx context.Context, src string, dst string, opt pairStorageMove) (err error) {
rs := s.getAbsPath(src)
rd := s.getAbsPath(dst)
srcPath := "/" + service.StringValue(s.properties.BucketName) + "/" + url.QueryEscape(rs)
_, err = s.bucket.PutObjectWithContext(ctx, rd, &service.PutObjectInput{
XQSMoveSource: &srcPath,
})
if err != nil {
return
}
return nil
}
func (s *Storage) nextObjectPageByDir(ctx context.Context, page *ObjectPage) error {
input := page.Status.(*objectPageStatus)
output, err := s.bucket.ListObjectsWithContext(ctx, &service.ListObjectsInput{
Delimiter: &input.delimiter,
Limit: &input.limit,
Marker: &input.marker,
Prefix: &input.prefix,
})
if err != nil {
return err
}
for _, v := range output.CommonPrefixes {
o := s.newObject(true)
o.ID = *v
o.Path = s.getRelPath(*v)
o.Mode |= ModeDir
page.Data = append(page.Data, o)
}
for _, v := range output.Keys {
// add filter to exclude dir-key itself, which would exist if created in console, see issue #365
if convert.StringValue(v.Key) == input.prefix {
continue
}
o, err := s.formatFileObject(v)
if err != nil {
return err
}
page.Data = append(page.Data, o)
}
if service.StringValue(output.NextMarker) == "" {
return IterateDone
}
if !service.BoolValue(output.HasMore) {
return IterateDone
}
if len(output.Keys) == 0 {
return IterateDone
}
input.marker = *output.NextMarker
return nil
}
func (s *Storage) nextObjectPageByPrefix(ctx context.Context, page *ObjectPage) error {
input := page.Status.(*objectPageStatus)
output, err := s.bucket.ListObjectsWithContext(ctx, &service.ListObjectsInput{
Limit: &input.limit,
Marker: &input.marker,
Prefix: &input.prefix,
})
if err != nil {
return err
}
for _, v := range output.Keys {
o, err := s.formatFileObject(v)
if err != nil {
return err
}
page.Data = append(page.Data, o)
}
if service.StringValue(output.NextMarker) == "" {
return IterateDone
}
if !service.BoolValue(output.HasMore) {
return IterateDone
}
if len(output.Keys) == 0 {
return IterateDone
}
input.marker = *output.NextMarker
return nil
}
func (s *Storage) nextPartObjectPageByPrefix(ctx context.Context, page *ObjectPage) error {
input := page.Status.(*objectPageStatus)
output, err := s.bucket.ListMultipartUploadsWithContext(ctx, &service.ListMultipartUploadsInput{
KeyMarker: &input.marker,
Limit: &input.limit,
Prefix: &input.prefix,
UploadIDMarker: &input.partIdMarker,
})
if err != nil {
return err
}
for _, v := range output.Uploads {
o := s.newObject(true)
o.ID = *v.Key
o.Path = s.getRelPath(*v.Key)
o.Mode |= ModePart
o.SetMultipartID(*v.UploadID)
page.Data = append(page.Data, o)
}
nextKeyMarker := service.StringValue(output.NextKeyMarker)
nextUploadIDMarker := service.StringValue(output.NextUploadIDMarker)
if nextKeyMarker == "" && nextUploadIDMarker == "" {
return IterateDone
}
if !service.BoolValue(output.HasMore) {
return IterateDone
}
input.marker = nextKeyMarker
input.partIdMarker = nextUploadIDMarker
return nil
}
func (s *Storage) nextPartPage(ctx context.Context, page *PartPage) error {
input := page.Status.(*partPageStatus)
output, err := s.bucket.ListMultipartWithContext(ctx, input.prefix, &service.ListMultipartInput{
Limit: &input.limit,
PartNumberMarker: &input.partNumberMarker,
UploadID: &input.uploadID,
})
if err != nil {
return err
}
for _, v := range output.ObjectParts {
p := &Part{
Index: *v.PartNumber,
Size: *v.Size,
ETag: service.StringValue(v.Etag),
}
page.Data = append(page.Data, p)
}
// FIXME: QingStor ListMulitpart API looks like buggy.
offset := input.partNumberMarker + len(output.ObjectParts)
if offset >= service.IntValue(output.Count) {
return IterateDone
}
input.partNumberMarker = offset
return nil
}
func (s *Storage) querySignHTTPDelete(ctx context.Context, path string, expire time.Duration, opt pairStorageQuerySignHTTPDelete) (req *http.Request, err error) {
panic("not implemented")
}
func (s *Storage) querySignHTTPRead(ctx context.Context, path string, expire time.Duration, opt pairStorageQuerySignHTTPRead) (req *http.Request, err error) {
pairs, err := s.parsePairStorageRead(opt.pairs)
if err != nil {
return
}
input, err := s.formatGetObjectInput(pairs)
if err != nil {
return
}
bucket := s.bucket.(*service.Bucket)
rp := s.getAbsPath(path)
r, _, err := bucket.GetObjectRequest(rp, input)
if err != nil {
return
}
if err = r.BuildWithContext(ctx); err != nil {
return
}
if err = r.SignQuery(int(expire.Seconds())); err != nil {
return
}
return r.HTTPRequest, nil
}
func (s *Storage) querySignHTTPWrite(ctx context.Context, path string, size int64, expire time.Duration, opt pairStorageQuerySignHTTPWrite) (req *http.Request, err error) {
pairs, err := s.parsePairStorageWrite(opt.pairs)
if err != nil {
return
}
input, err := s.formatPutObjectInput(size, pairs)
if err != nil {
return
}
bucket := s.bucket.(*service.Bucket)
rp := s.getAbsPath(path)
r, _, err := bucket.PutObjectRequest(rp, input)
if err != nil {
return
}
if err = r.BuildWithContext(ctx); err != nil {
return
}
if err = r.SignQuery(int(expire.Seconds())); err != nil {
return
}
return r.HTTPRequest, nil
}
func (s *Storage) reach(ctx context.Context, path string, opt pairStorageReach) (url string, err error) {
// FIXME: sdk should export GetObjectRequest as interface too?
bucket := s.bucket.(*service.Bucket)
rp := s.getAbsPath(path)
r, _, err := bucket.GetObjectRequest(rp, nil)
if err != nil {
return
}
if err = r.BuildWithContext(ctx); err != nil {
return
}
expire := opt.Expire
if err = r.SignQuery(int(expire.Seconds())); err != nil {
return
}
return r.HTTPRequest.URL.String(), nil
}
func (s *Storage) read(ctx context.Context, path string, w io.Writer, opt pairStorageRead) (n int64, err error) {
input, err := s.formatGetObjectInput(opt)
if err != nil {
return
}
rp := s.getAbsPath(path)
output, err := s.bucket.GetObjectWithContext(ctx, rp, input)
if err != nil {
return n, err
}
defer output.Body.Close()
rc := output.Body
if opt.HasIoCallback {
rc = iowrap.CallbackReadCloser(rc, opt.IoCallback)
}
return io.Copy(w, rc)
}
func (s *Storage) stat(ctx context.Context, path string, opt pairStorageStat) (o *Object, err error) {
rp := s.getAbsPath(path)
if opt.HasMultipartID {
input := &service.ListMultipartInput{
UploadID: service.String(opt.MultipartID),
Limit: service.Int(0),
}
_, err := s.bucket.ListMultipartWithContext(ctx, rp, input)
if err != nil {
return nil, err
}
o = s.newObject(true)
o.ID = rp
o.Path = path
o.Mode |= ModePart
o.SetMultipartID(opt.MultipartID)
return o, nil
}
if opt.HasObjectMode && opt.ObjectMode.IsDir() {
if !s.features.VirtualDir {
err = services.PairUnsupportedError{Pair: ps.WithObjectMode(opt.ObjectMode)}
return
}
rp += "/"
}
input := &service.HeadObjectInput{}
output, err := s.bucket.HeadObjectWithContext(ctx, rp, input)
if err != nil {
return
}
o = s.newObject(true)
o.ID = rp
o.Path = path
if output.XQSMetaData != nil {
metadata := *output.XQSMetaData
// By calling `HeadObject`, the first letter of the `key` of the object metadata will be capitalized.
if v, ok := metadata[metadataLinkTargetHeader]; ok {
// The path is a symlink object.
if !s.features.VirtualLink {
// The virtual link is not enabled, so we set the object mode to `ModeRead`.
o.Mode |= ModeRead
} else {
// qingstor does not have an absolute path, so when we call `getAbsPath`, it will remove the prefix `/`.
// To ensure that the path matches the one the user gets, we should re-add `/` here.
o.SetLinkTarget("/" + v)
o.Mode |= ModeLink
}
}
}
if o.Mode&ModeLink == 0 && o.Mode&ModeRead == 0 {
if opt.HasObjectMode && opt.ObjectMode.IsDir() {
o.Mode |= ModeDir
} else {
o.Mode |= ModeRead
}
}
o.SetContentLength(service.Int64Value(output.ContentLength))
o.SetLastModified(service.TimeValue(output.LastModified))
if output.ContentType != nil {
o.SetContentType(service.StringValue(output.ContentType))
}
if output.ETag != nil {
o.SetEtag(service.StringValue(output.ETag))
}
var sm ObjectSystemMetadata
if v := service.StringValue(output.XQSStorageClass); v != "" {
sm.StorageClass = v
}
if v := service.StringValue(output.XQSEncryptionCustomerAlgorithm); v != "" {
sm.EncryptionCustomerAlgorithm = v
}
o.SetSystemMetadata(sm)
return o, nil
}
func (s *Storage) write(ctx context.Context, path string, r io.Reader, size int64, opt pairStorageWrite) (n int64, err error) {
if size > writeSizeMaximum {
err = fmt.Errorf("size limit exceeded: %w", services.ErrRestrictionDissatisfied)
return
}
// According to GSP-751, we should allow the user to pass in a nil io.Reader.
// ref: https://github.com/beyondstorage/go-storage/blob/master/docs/rfcs/751-write-empty-file-behavior.md
if r == nil && size != 0 {
return 0, fmt.Errorf("reader is nil but size is not 0")
}
if opt.HasIoCallback {
r = iowrap.CallbackReader(r, opt.IoCallback)
}
input, err := s.formatPutObjectInput(size, opt)
if err != nil {
return
}
input.Body = io.LimitReader(r, size)
rp := s.getAbsPath(path)
_, err = s.bucket.PutObjectWithContext(ctx, rp, input)
if err != nil {
return
}
return size, nil
}
func (s *Storage) writeAppend(ctx context.Context, o *Object, r io.Reader, size int64, opt pairStorageWriteAppend) (n int64, err error) {
if size > appendSizeMaximum {
err = fmt.Errorf("size limit exceeded: %w", services.ErrRestrictionDissatisfied)
return
}
rp := o.GetID()
offset, _ := o.GetAppendOffset()
input := &service.AppendObjectInput{
Position: &offset,
ContentLength: &size,
Body: io.LimitReader(r, size),
}
if opt.HasContentMd5 {
input.ContentMD5 = &opt.ContentMd5
}
output, err := s.bucket.AppendObjectWithContext(ctx, rp, input)
if err != nil {
return
}
if output == nil || output.XQSNextAppendPosition == nil {
err = ErrAppendNextPositionEmpty
return
} else {
offset = *output.XQSNextAppendPosition
}
// We should reset the offset after calling `AppendObject` to prevent the offset being changed.
o.SetAppendOffset(offset)
return size, nil
}
func (s *Storage) writeMultipart(ctx context.Context, o *Object, r io.Reader, size int64, index int, opt pairStorageWriteMultipart) (n int64, part *Part, err error) {
if index < multipartNumberMinimum || index > multipartNumberMaximum {
err = ErrPartNumberInvalid
return
}
if size > multipartSizeMaximum {
err = fmt.Errorf("size limit exceeded: %w", services.ErrRestrictionDissatisfied)
return
}
if opt.HasIoCallback {
r = iowrap.CallbackReader(r, opt.IoCallback)
}
input := &service.UploadMultipartInput{
PartNumber: service.Int(index),
UploadID: service.String(o.MustGetMultipartID()),
ContentLength: &size,
Body: io.LimitReader(r, size),
}
if opt.HasEncryptionCustomerAlgorithm {
input.XQSEncryptionCustomerAlgorithm, input.XQSEncryptionCustomerKey, input.XQSEncryptionCustomerKeyMD5, err = calculateEncryptionHeaders(opt.EncryptionCustomerAlgorithm, opt.EncryptionCustomerKey)
if err != nil {
return
}
}
output, err := s.bucket.UploadMultipartWithContext(ctx, o.ID, input)
if err != nil {
return
}
part = &Part{
Index: index,
Size: size,
ETag: service.StringValue(output.ETag),
}
return size, part, nil
}