FPutObject - Create an object in a bucket, with contents from file at filePath. Allows request cancellation.
(ctx context.Context, bucketName, objectName, filePath string, opts PutObjectOptions)
| 28 | |
| 29 | // FPutObject - Create an object in a bucket, with contents from file at filePath. Allows request cancellation. |
| 30 | func (c *Client) FPutObject(ctx context.Context, bucketName, objectName, filePath string, opts PutObjectOptions) (info UploadInfo, err error) { |
| 31 | // Input validation. |
| 32 | if err := s3utils.CheckValidBucketName(bucketName); err != nil { |
| 33 | return UploadInfo{}, err |
| 34 | } |
| 35 | if err := s3utils.CheckValidObjectName(objectName); err != nil { |
| 36 | return UploadInfo{}, err |
| 37 | } |
| 38 | |
| 39 | // Open the referenced file. |
| 40 | fileReader, err := os.Open(filePath) |
| 41 | // If any error fail quickly here. |
| 42 | if err != nil { |
| 43 | return UploadInfo{}, err |
| 44 | } |
| 45 | defer fileReader.Close() |
| 46 | |
| 47 | // Save the file stat. |
| 48 | fileStat, err := fileReader.Stat() |
| 49 | if err != nil { |
| 50 | return UploadInfo{}, err |
| 51 | } |
| 52 | |
| 53 | // Save the file size. |
| 54 | fileSize := fileStat.Size() |
| 55 | |
| 56 | // Set contentType based on filepath extension if not given or default |
| 57 | // value of "application/octet-stream" if the extension has no associated type. |
| 58 | if opts.ContentType == "" { |
| 59 | if opts.ContentType = mime.TypeByExtension(filepath.Ext(filePath)); opts.ContentType == "" { |
| 60 | opts.ContentType = "application/octet-stream" |
| 61 | } |
| 62 | } |
| 63 | return c.PutObject(ctx, bucketName, objectName, fileReader, fileSize, opts) |
| 64 | } |
no test coverage detected