api-get-object-file.go 3.4 KB

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