api-put-object-common.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * MinIO Go Library for Amazon S3 Compatible Cloud Storage
  3. * Copyright 2015-2017 MinIO, Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package s3cli
  18. import (
  19. "context"
  20. "io"
  21. "math"
  22. "os"
  23. "github.com/minio/minio-go/v6/pkg/s3utils"
  24. )
  25. // Verify if reader is *minio.Object
  26. func isObject(reader io.Reader) (ok bool) {
  27. _, ok = reader.(*Object)
  28. return
  29. }
  30. // Verify if reader is a generic ReaderAt
  31. func isReadAt(reader io.Reader) (ok bool) {
  32. var v *os.File
  33. v, ok = reader.(*os.File)
  34. if ok {
  35. // Stdin, Stdout and Stderr all have *os.File type
  36. // which happen to also be io.ReaderAt compatible
  37. // we need to add special conditions for them to
  38. // be ignored by this function.
  39. for _, f := range []string{
  40. "/dev/stdin",
  41. "/dev/stdout",
  42. "/dev/stderr",
  43. } {
  44. if f == v.Name() {
  45. ok = false
  46. break
  47. }
  48. }
  49. } else {
  50. _, ok = reader.(io.ReaderAt)
  51. }
  52. return
  53. }
  54. // optimalPartInfo - calculate the optimal part info for a given
  55. // object size.
  56. //
  57. // NOTE: Assumption here is that for any object to be uploaded to any S3 compatible
  58. // object storage it will have the following parameters as constants.
  59. //
  60. // maxPartsCount - 10000
  61. // minPartSize - 128MiB
  62. // maxMultipartPutObjectSize - 5TiB
  63. //
  64. func optimalPartInfo(objectSize int64, configuredPartSize uint64) (totalPartsCount int, partSize int64, lastPartSize int64, err error) {
  65. // object size is '-1' set it to 5TiB.
  66. if objectSize == -1 {
  67. objectSize = maxMultipartPutObjectSize
  68. }
  69. // object size is larger than supported maximum.
  70. if objectSize > maxMultipartPutObjectSize {
  71. err = ErrEntityTooLarge(objectSize, maxMultipartPutObjectSize, "", "")
  72. return
  73. }
  74. var partSizeFlt float64
  75. if configuredPartSize > 0 {
  76. if int64(configuredPartSize) > objectSize {
  77. err = ErrEntityTooLarge(int64(configuredPartSize), objectSize, "", "")
  78. return
  79. }
  80. if objectSize > (int64(configuredPartSize) * maxPartsCount) {
  81. err = ErrInvalidArgument("Part size * max_parts(10000) is lesser than input objectSize.")
  82. return
  83. }
  84. if configuredPartSize < absMinPartSize {
  85. err = ErrInvalidArgument("Input part size is smaller than allowed minimum of 5MiB.")
  86. return
  87. }
  88. if configuredPartSize > maxPartSize {
  89. err = ErrInvalidArgument("Input part size is bigger than allowed maximum of 5GiB.")
  90. return
  91. }
  92. partSizeFlt = float64(configuredPartSize)
  93. } else {
  94. configuredPartSize = minPartSize
  95. // Use floats for part size for all calculations to avoid
  96. // overflows during float64 to int64 conversions.
  97. partSizeFlt = float64(objectSize / maxPartsCount)
  98. partSizeFlt = math.Ceil(partSizeFlt/float64(configuredPartSize)) * float64(configuredPartSize)
  99. }
  100. // Total parts count.
  101. totalPartsCount = int(math.Ceil(float64(objectSize) / partSizeFlt))
  102. // Part size.
  103. partSize = int64(partSizeFlt)
  104. // Last part size.
  105. lastPartSize = objectSize - int64(totalPartsCount-1)*partSize
  106. return totalPartsCount, partSize, lastPartSize, nil
  107. }
  108. // getUploadID - fetch upload id if already present for an object name
  109. // or initiate a new request to fetch a new upload id.
  110. func (c Client) newUploadID(ctx context.Context, bucketName, objectName string, opts PutObjectOptions) (uploadID string, err error) {
  111. // Input validation.
  112. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  113. return "", err
  114. }
  115. if err := s3utils.CheckValidObjectName(objectName); err != nil {
  116. return "", err
  117. }
  118. // Initiate multipart upload for an object.
  119. initMultipartUploadResult, err := c.InitiateMultipartUpload(ctx, bucketName, objectName, opts)
  120. if err != nil {
  121. return "", err
  122. }
  123. return initMultipartUploadResult.UploadID, nil
  124. }