FGetObject - download contents of an object to a local file. The options can be used to specify the GET request further.
(ctx context.Context, bucketName, objectName, filePath string, opts GetObjectOptions)
| 29 | // FGetObject - download contents of an object to a local file. |
| 30 | // The options can be used to specify the GET request further. |
| 31 | func (c *Client) FGetObject(ctx context.Context, bucketName, objectName, filePath string, opts GetObjectOptions) error { |
| 32 | // Input validation. |
| 33 | if err := s3utils.CheckValidBucketName(bucketName); err != nil { |
| 34 | return err |
| 35 | } |
| 36 | if err := s3utils.CheckValidObjectName(objectName); err != nil { |
| 37 | return err |
| 38 | } |
| 39 | |
| 40 | // Verify if destination already exists. |
| 41 | st, err := os.Stat(filePath) |
| 42 | if err == nil { |
| 43 | // If the destination exists and is a directory. |
| 44 | if st.IsDir() { |
| 45 | return errInvalidArgument("fileName is a directory.") |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // Proceed if file does not exist. return for all other errors. |
| 50 | if err != nil { |
| 51 | if !os.IsNotExist(err) { |
| 52 | return err |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Extract top level directory. |
| 57 | objectDir, _ := filepath.Split(filePath) |
| 58 | if objectDir != "" { |
| 59 | // Create any missing top level directories. |
| 60 | if err := os.MkdirAll(objectDir, 0o700); err != nil { |
| 61 | return err |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Gather md5sum. |
| 66 | objectStat, err := c.StatObject(ctx, bucketName, objectName, StatObjectOptions(opts)) |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | |
| 71 | // Write to a temporary file "fileName.part.minio" before saving. |
| 72 | filePartPath := filepath.Join(filepath.Dir(filePath), sum256Hex([]byte(filepath.Base(filePath)+objectStat.ETag))+".part.minio") |
| 73 | |
| 74 | // If exists, open in append mode. If not create it as a part file. |
| 75 | filePart, err := os.OpenFile(filePartPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | // If we return early with an error, be sure to close and delete |
| 81 | // filePart. If we have an error along the way there is a chance |
| 82 | // that filePart is somehow damaged, and we should discard it. |
| 83 | closeAndRemove := true |
| 84 | defer func() { |
| 85 | if closeAndRemove { |
| 86 | _ = filePart.Close() |
| 87 | _ = os.Remove(filePartPath) |
| 88 | } |