api-put-object-common.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 minio
  18. import (
  19. "context"
  20. "io"
  21. "math"
  22. "os"
  23. "github.com/minio/minio-go/v7/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 - 16MiB
  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. var unknownSize bool
  67. if objectSize == -1 {
  68. unknownSize = true
  69. objectSize = maxMultipartPutObjectSize
  70. }
  71. // object size is larger than supported maximum.
  72. if objectSize > maxMultipartPutObjectSize {
  73. err = errEntityTooLarge(objectSize, maxMultipartPutObjectSize, "", "")
  74. return
  75. }
  76. var partSizeFlt float64
  77. if configuredPartSize > 0 {
  78. if int64(configuredPartSize) > objectSize {
  79. err = errEntityTooLarge(int64(configuredPartSize), objectSize, "", "")
  80. return
  81. }
  82. if !unknownSize {
  83. if objectSize > (int64(configuredPartSize) * maxPartsCount) {
  84. err = errInvalidArgument("Part size * max_parts(10000) is lesser than input objectSize.")
  85. return
  86. }
  87. }
  88. if configuredPartSize < absMinPartSize {
  89. err = errInvalidArgument("Input part size is smaller than allowed minimum of 5MiB.")
  90. return
  91. }
  92. if configuredPartSize > maxPartSize {
  93. err = errInvalidArgument("Input part size is bigger than allowed maximum of 5GiB.")
  94. return
  95. }
  96. partSizeFlt = float64(configuredPartSize)
  97. if unknownSize {
  98. // If input has unknown size and part size is configured
  99. // keep it to maximum allowed as per 10000 parts.
  100. objectSize = int64(configuredPartSize) * maxPartsCount
  101. }
  102. } else {
  103. configuredPartSize = minPartSize
  104. // Use floats for part size for all calculations to avoid
  105. // overflows during float64 to int64 conversions.
  106. partSizeFlt = float64(objectSize / maxPartsCount)
  107. partSizeFlt = math.Ceil(partSizeFlt/float64(configuredPartSize)) * float64(configuredPartSize)
  108. }
  109. // Total parts count.
  110. totalPartsCount = int(math.Ceil(float64(objectSize) / partSizeFlt))
  111. // Part size.
  112. partSize = int64(partSizeFlt)
  113. // Last part size.
  114. lastPartSize = objectSize - int64(totalPartsCount-1)*partSize
  115. return totalPartsCount, partSize, lastPartSize, nil
  116. }
  117. // getUploadID - fetch upload id if already present for an object name
  118. // or initiate a new request to fetch a new upload id.
  119. func (c Client) newUploadID(ctx context.Context, bucketName, objectName string, opts PutObjectOptions) (uploadID string, err error) {
  120. // Input validation.
  121. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  122. return "", err
  123. }
  124. if err := s3utils.CheckValidObjectName(objectName); err != nil {
  125. return "", err
  126. }
  127. // Initiate multipart upload for an object.
  128. initMultipartUploadResult, err := c.initiateMultipartUpload(ctx, bucketName, objectName, opts)
  129. if err != nil {
  130. return "", err
  131. }
  132. return initMultipartUploadResult.UploadID, nil
  133. }