api-put-object-common.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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/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. _, ok = reader.(io.ReaderAt)
  33. if ok {
  34. var v *os.File
  35. v, ok = reader.(*os.File)
  36. if ok {
  37. // Stdin, Stdout and Stderr all have *os.File type
  38. // which happen to also be io.ReaderAt compatible
  39. // we need to add special conditions for them to
  40. // be ignored by this function.
  41. for _, f := range []string{
  42. "/dev/stdin",
  43. "/dev/stdout",
  44. "/dev/stderr",
  45. } {
  46. if f == v.Name() {
  47. ok = false
  48. break
  49. }
  50. }
  51. }
  52. }
  53. return
  54. }
  55. // optimalPartInfo - calculate the optimal part info for a given
  56. // object size.
  57. //
  58. // NOTE: Assumption here is that for any object to be uploaded to any S3 compatible
  59. // object storage it will have the following parameters as constants.
  60. //
  61. // maxPartsCount - 10000
  62. // minPartSize - 64MiB
  63. // maxMultipartPutObjectSize - 5TiB
  64. //
  65. func optimalPartInfo(objectSize int64) (totalPartsCount int, partSize int64, lastPartSize int64, err error) {
  66. // object size is '-1' set it to 5TiB.
  67. if objectSize == -1 {
  68. objectSize = maxMultipartPutObjectSize
  69. }
  70. // object size is larger than supported maximum.
  71. if objectSize > maxMultipartPutObjectSize {
  72. err = ErrEntityTooLarge(objectSize, maxMultipartPutObjectSize, "", "")
  73. return
  74. }
  75. // Use floats for part size for all calculations to avoid
  76. // overflows during float64 to int64 conversions.
  77. partSizeFlt := math.Ceil(float64(objectSize / maxPartsCount))
  78. partSizeFlt = math.Ceil(partSizeFlt/minPartSize) * minPartSize
  79. // Total parts count.
  80. totalPartsCount = int(math.Ceil(float64(objectSize) / partSizeFlt))
  81. // Part size.
  82. partSize = int64(partSizeFlt)
  83. // Last part size.
  84. lastPartSize = objectSize - int64(totalPartsCount-1)*partSize
  85. return totalPartsCount, partSize, lastPartSize, nil
  86. }
  87. // getUploadID - fetch upload id if already present for an object name
  88. // or initiate a new request to fetch a new upload id.
  89. func (c Client) newUploadID(ctx context.Context, bucketName, objectName string, opts PutObjectOptions) (uploadID string, err error) {
  90. // Input validation.
  91. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  92. return "", err
  93. }
  94. if err := s3utils.CheckValidObjectName(objectName); err != nil {
  95. return "", err
  96. }
  97. // Initiate multipart upload for an object.
  98. initMultipartUploadResult, err := c.initiateMultipartUpload(ctx, bucketName, objectName, opts)
  99. if err != nil {
  100. return "", err
  101. }
  102. return initMultipartUploadResult.UploadID, nil
  103. }