multipart.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package handlers
  15. import (
  16. "context"
  17. "net/http"
  18. "sort"
  19. "yunion.io/x/cloudmux/pkg/cloudprovider"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/s3cli"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. "yunion.io/x/onecloud/pkg/s3gateway/models"
  24. )
  25. func initMultipartUpload(ctx context.Context, userCred mcclient.TokenCredential, hdr http.Header, bucketName string, key string) (*s3cli.InitiateMultipartUploadResult, http.Header, error) {
  26. bucket, err := models.BucketManager.GetByName(ctx, userCred, bucketName)
  27. if err != nil {
  28. return nil, nil, errors.Wrap(err, "models.BucketManager.GetByName")
  29. }
  30. iBucket, err := bucket.GetIBucket(ctx, userCred)
  31. if err != nil {
  32. return nil, nil, errors.Wrap(err, "bucket.GetIBucket")
  33. }
  34. meta := cloudprovider.FetchMetaFromHttpHeader(cloudprovider.META_HEADER_PREFIX, hdr)
  35. aclStr := hdr.Get(http.CanonicalHeaderKey("x-amz-acl"))
  36. storageClassStr := hdr.Get(http.CanonicalHeaderKey("x-amz-storage-class"))
  37. uploadId, err := iBucket.NewMultipartUpload(ctx, key, cloudprovider.TBucketACLType(aclStr), storageClassStr, meta)
  38. if err != nil {
  39. return nil, nil, errors.Wrap(err, "NewMultipartUpload")
  40. }
  41. result := s3cli.InitiateMultipartUploadResult{}
  42. result.Bucket = bucketName
  43. result.Key = key
  44. result.UploadID = uploadId
  45. return &result, nil, nil
  46. }
  47. type SMultiparts []s3cli.CompletePart
  48. func (a SMultiparts) Len() int { return len(a) }
  49. func (a SMultiparts) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  50. func (a SMultiparts) Less(i, j int) bool { return a[i].PartNumber < a[j].PartNumber }
  51. func completeMultipartUpload(ctx context.Context, userCred mcclient.TokenCredential, hdr http.Header, bucketName string, key string, uploadId string, request *s3cli.CompleteMultipartUpload) (*s3cli.CompleteMultipartUploadResult, http.Header, error) {
  52. bucket, err := models.BucketManager.GetByName(ctx, userCred, bucketName)
  53. if err != nil {
  54. return nil, nil, errors.Wrap(err, "models.BucketManager.GetByName")
  55. }
  56. iBucket, err := bucket.GetIBucket(ctx, userCred)
  57. if err != nil {
  58. return nil, nil, errors.Wrap(err, "bucket.GetIBucket")
  59. }
  60. sort.Sort(SMultiparts(request.Parts))
  61. partEtags := make([]string, len(request.Parts))
  62. for i := range request.Parts {
  63. partEtags[i] = request.Parts[i].ETag
  64. }
  65. err = iBucket.CompleteMultipartUpload(ctx, key, uploadId, partEtags)
  66. if err != nil {
  67. return nil, nil, errors.Wrap(err, "CompleteMultipartUpload")
  68. }
  69. obj, err := cloudprovider.GetIObject(iBucket, key)
  70. if err != nil {
  71. return nil, nil, errors.Wrap(err, "cloudprovider.GetIObject")
  72. }
  73. result := s3cli.CompleteMultipartUploadResult{}
  74. result.Bucket = bucketName
  75. result.Key = key
  76. result.ETag = obj.GetETag()
  77. result.Location = iBucket.GetLocation()
  78. return &result, nil, nil
  79. }