api-get-object-file.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. "os"
  22. "path/filepath"
  23. "github.com/minio/minio-go/v6/pkg/s3utils"
  24. )
  25. // FGetObjectWithContext - download contents of an object to a local file.
  26. // The options can be used to specify the GET request further.
  27. func (c Client) FGetObjectWithContext(ctx context.Context, bucketName, objectName, filePath string, opts GetObjectOptions) error {
  28. return c.fGetObjectWithContext(ctx, bucketName, objectName, filePath, opts)
  29. }
  30. // FGetObject - download contents of an object to a local file.
  31. func (c Client) FGetObject(bucketName, objectName, filePath string, opts GetObjectOptions) error {
  32. return c.fGetObjectWithContext(context.Background(), bucketName, objectName, filePath, opts)
  33. }
  34. // fGetObjectWithContext - fgetObject wrapper function with context
  35. func (c Client) fGetObjectWithContext(ctx context.Context, bucketName, objectName, filePath string, opts GetObjectOptions) error {
  36. // Input validation.
  37. if err := s3utils.CheckValidBucketName(bucketName); err != nil {
  38. return err
  39. }
  40. if err := s3utils.CheckValidObjectName(objectName); err != nil {
  41. return err
  42. }
  43. // Verify if destination already exists.
  44. st, err := os.Stat(filePath)
  45. if err == nil {
  46. // If the destination exists and is a directory.
  47. if st.IsDir() {
  48. return ErrInvalidArgument("fileName is a directory.")
  49. }
  50. }
  51. // Proceed if file does not exist. return for all other errors.
  52. if err != nil {
  53. if !os.IsNotExist(err) {
  54. return err
  55. }
  56. }
  57. // Extract top level directory.
  58. objectDir, _ := filepath.Split(filePath)
  59. if objectDir != "" {
  60. // Create any missing top level directories.
  61. if err := os.MkdirAll(objectDir, 0700); err != nil {
  62. return err
  63. }
  64. }
  65. // Gather md5sum.
  66. objectStat, err := c.StatObject(bucketName, objectName, StatObjectOptions{opts})
  67. if err != nil {
  68. return err
  69. }
  70. // Write to a temporary file "fileName.part.minio" before saving.
  71. filePartPath := filePath + objectStat.ETag + ".part.minio"
  72. // If exists, open in append mode. If not create it as a part file.
  73. filePart, err := os.OpenFile(filePartPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
  74. if err != nil {
  75. return err
  76. }
  77. // Issue Stat to get the current offset.
  78. st, err = filePart.Stat()
  79. if err != nil {
  80. return err
  81. }
  82. // Initialize get object request headers to set the
  83. // appropriate range offsets to read from.
  84. if st.Size() > 0 {
  85. opts.SetRange(st.Size(), 0)
  86. }
  87. // Seek to current position for incoming reader.
  88. objectReader, objectStat, err := c.getObject(ctx, bucketName, objectName, opts)
  89. if err != nil {
  90. return err
  91. }
  92. // Write to the part file.
  93. if _, err = io.CopyN(filePart, objectReader, objectStat.Size); err != nil {
  94. return err
  95. }
  96. // Close the file before rename, this is specifically needed for Windows users.
  97. if err = filePart.Close(); err != nil {
  98. return err
  99. }
  100. // Safely completed. Now commit by renaming to actual filename.
  101. if err = os.Rename(filePartPath, filePath); err != nil {
  102. return err
  103. }
  104. // Return.
  105. return nil
  106. }