2015-12-28 20:23:53 +00:00
|
|
|
/*
|
2016-04-18 19:27:13 +00:00
|
|
|
* Minio Go Library for Amazon S3 Compatible Cloud Storage (C) 2015, 2016 Minio, Inc.
|
2015-12-28 20:23:53 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package minio
|
|
|
|
|
|
|
|
import (
|
2016-07-29 18:28:44 +00:00
|
|
|
"crypto/md5"
|
|
|
|
"crypto/sha256"
|
2015-12-28 20:23:53 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2016-07-29 18:28:44 +00:00
|
|
|
"hash"
|
2015-12-28 20:23:53 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2016-04-18 19:27:13 +00:00
|
|
|
"mime"
|
2015-12-28 20:23:53 +00:00
|
|
|
"os"
|
2016-04-18 19:27:13 +00:00
|
|
|
"path/filepath"
|
2015-12-28 20:23:53 +00:00
|
|
|
"sort"
|
|
|
|
)
|
|
|
|
|
2016-01-07 19:23:38 +00:00
|
|
|
// FPutObject - Create an object in a bucket, with contents from file at filePath.
|
|
|
|
func (c Client) FPutObject(bucketName, objectName, filePath, contentType string) (n int64, err error) {
|
2015-12-28 20:23:53 +00:00
|
|
|
// Input validation.
|
|
|
|
if err := isValidBucketName(bucketName); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if err := isValidObjectName(objectName); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the referenced file.
|
2016-01-07 19:23:38 +00:00
|
|
|
fileReader, err := os.Open(filePath)
|
2015-12-28 20:23:53 +00:00
|
|
|
// If any error fail quickly here.
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2016-01-07 19:23:38 +00:00
|
|
|
defer fileReader.Close()
|
2015-12-28 20:23:53 +00:00
|
|
|
|
|
|
|
// Save the file stat.
|
2016-01-07 19:23:38 +00:00
|
|
|
fileStat, err := fileReader.Stat()
|
2015-12-28 20:23:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the file size.
|
|
|
|
fileSize := fileStat.Size()
|
2016-01-07 19:23:38 +00:00
|
|
|
|
|
|
|
// Check for largest object size allowed.
|
2015-12-28 20:23:53 +00:00
|
|
|
if fileSize > int64(maxMultipartPutObjectSize) {
|
2016-01-27 22:18:23 +00:00
|
|
|
return 0, ErrEntityTooLarge(fileSize, maxMultipartPutObjectSize, bucketName, objectName)
|
2015-12-28 20:23:53 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 19:27:13 +00:00
|
|
|
// Set contentType based on filepath extension if not given or default
|
|
|
|
// value of "binary/octet-stream" if the extension has no associated type.
|
|
|
|
if contentType == "" {
|
|
|
|
if contentType = mime.TypeByExtension(filepath.Ext(filePath)); contentType == "" {
|
|
|
|
contentType = "application/octet-stream"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-28 20:23:53 +00:00
|
|
|
// NOTE: Google Cloud Storage multipart Put is not compatible with Amazon S3 APIs.
|
|
|
|
// Current implementation will only upload a maximum of 5GiB to Google Cloud Storage servers.
|
|
|
|
if isGoogleEndpoint(c.endpointURL) {
|
2016-01-02 13:37:08 +00:00
|
|
|
if fileSize > int64(maxSinglePutObjectSize) {
|
2015-12-28 20:23:53 +00:00
|
|
|
return 0, ErrorResponse{
|
|
|
|
Code: "NotImplemented",
|
|
|
|
Message: fmt.Sprintf("Invalid Content-Length %d for file uploads to Google Cloud Storage.", fileSize),
|
|
|
|
Key: objectName,
|
|
|
|
BucketName: bucketName,
|
|
|
|
}
|
|
|
|
}
|
2016-01-27 22:18:23 +00:00
|
|
|
// Do not compute MD5 for Google Cloud Storage. Uploads up to 5GiB in size.
|
|
|
|
return c.putObjectNoChecksum(bucketName, objectName, fileReader, fileSize, contentType, nil)
|
2015-12-28 20:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: S3 doesn't allow anonymous multipart requests.
|
|
|
|
if isAmazonEndpoint(c.endpointURL) && c.anonymous {
|
2016-01-02 13:37:08 +00:00
|
|
|
if fileSize > int64(maxSinglePutObjectSize) {
|
2015-12-28 20:23:53 +00:00
|
|
|
return 0, ErrorResponse{
|
|
|
|
Code: "NotImplemented",
|
|
|
|
Message: fmt.Sprintf("For anonymous requests Content-Length cannot be %d.", fileSize),
|
|
|
|
Key: objectName,
|
|
|
|
BucketName: bucketName,
|
|
|
|
}
|
|
|
|
}
|
2016-01-27 22:18:23 +00:00
|
|
|
// Do not compute MD5 for anonymous requests to Amazon
|
|
|
|
// S3. Uploads up to 5GiB in size.
|
|
|
|
return c.putObjectNoChecksum(bucketName, objectName, fileReader, fileSize, contentType, nil)
|
2015-12-28 20:23:53 +00:00
|
|
|
}
|
|
|
|
|
2016-01-02 13:37:08 +00:00
|
|
|
// Small object upload is initiated for uploads for input data size smaller than 5MiB.
|
2016-01-27 22:18:23 +00:00
|
|
|
if fileSize < minPartSize && fileSize >= 0 {
|
|
|
|
return c.putObjectSingle(bucketName, objectName, fileReader, fileSize, contentType, nil)
|
2015-12-28 20:23:53 +00:00
|
|
|
}
|
2016-01-07 19:23:38 +00:00
|
|
|
// Upload all large objects as multipart.
|
2016-01-27 22:18:23 +00:00
|
|
|
n, err = c.putObjectMultipartFromFile(bucketName, objectName, fileReader, fileSize, contentType, nil)
|
2015-12-28 20:23:53 +00:00
|
|
|
if err != nil {
|
2016-01-07 19:23:38 +00:00
|
|
|
errResp := ToErrorResponse(err)
|
|
|
|
// Verify if multipart functionality is not available, if not
|
|
|
|
// fall back to single PutObject operation.
|
|
|
|
if errResp.Code == "NotImplemented" {
|
|
|
|
// If size of file is greater than '5GiB' fail.
|
|
|
|
if fileSize > maxSinglePutObjectSize {
|
2016-01-27 22:18:23 +00:00
|
|
|
return 0, ErrEntityTooLarge(fileSize, maxSinglePutObjectSize, bucketName, objectName)
|
2016-01-07 19:23:38 +00:00
|
|
|
}
|
|
|
|
// Fall back to uploading as single PutObject operation.
|
2016-01-27 22:18:23 +00:00
|
|
|
return c.putObjectSingle(bucketName, objectName, fileReader, fileSize, contentType, nil)
|
2016-01-07 19:23:38 +00:00
|
|
|
}
|
|
|
|
return n, err
|
2015-12-28 20:23:53 +00:00
|
|
|
}
|
2016-01-07 19:23:38 +00:00
|
|
|
return n, nil
|
2015-12-28 20:23:53 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 19:23:38 +00:00
|
|
|
// putObjectMultipartFromFile - Creates object from contents of *os.File
|
|
|
|
//
|
|
|
|
// NOTE: This function is meant to be used for readers with local
|
|
|
|
// file as in *os.File. This function resumes by skipping all the
|
|
|
|
// necessary parts which were already uploaded by verifying them
|
|
|
|
// against MD5SUM of each individual parts. This function also
|
|
|
|
// effectively utilizes file system capabilities of reading from
|
|
|
|
// specific sections and not having to create temporary files.
|
2016-01-27 22:18:23 +00:00
|
|
|
func (c Client) putObjectMultipartFromFile(bucketName, objectName string, fileReader io.ReaderAt, fileSize int64, contentType string, progress io.Reader) (int64, error) {
|
2015-12-28 20:23:53 +00:00
|
|
|
// Input validation.
|
|
|
|
if err := isValidBucketName(bucketName); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if err := isValidObjectName(objectName); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2016-01-07 19:23:38 +00:00
|
|
|
// Get upload id for an object, initiates a new multipart request
|
2015-12-28 20:23:53 +00:00
|
|
|
// if it cannot find any previously partially uploaded object.
|
2016-01-08 20:01:06 +00:00
|
|
|
uploadID, isNew, err := c.getUploadID(bucketName, objectName, contentType)
|
2015-12-28 20:23:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2016-01-07 19:23:38 +00:00
|
|
|
// Total data read and written to server. should be equal to 'size' at the end of the call.
|
2015-12-28 20:23:53 +00:00
|
|
|
var totalUploadedSize int64
|
|
|
|
|
|
|
|
// Complete multipart upload.
|
|
|
|
var completeMultipartUpload completeMultipartUpload
|
|
|
|
|
2016-01-08 20:01:06 +00:00
|
|
|
// A map of all uploaded parts.
|
|
|
|
var partsInfo = make(map[int]objectPart)
|
|
|
|
|
|
|
|
// If this session is a continuation of a previous session fetch all
|
|
|
|
// previously uploaded parts info.
|
|
|
|
if !isNew {
|
|
|
|
// Fetch previously upload parts and maximum part size.
|
2016-01-27 22:18:23 +00:00
|
|
|
partsInfo, err = c.listObjectParts(bucketName, objectName, uploadID)
|
2016-01-08 20:01:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2015-12-28 20:23:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-27 22:18:23 +00:00
|
|
|
// Calculate the optimal parts info for a given size.
|
|
|
|
totalPartsCount, partSize, _, err := optimalPartInfo(fileSize)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2015-12-28 20:23:53 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 22:18:23 +00:00
|
|
|
// Part number always starts with '1'.
|
|
|
|
partNumber := 1
|
2016-01-02 13:37:08 +00:00
|
|
|
|
2016-01-27 22:18:23 +00:00
|
|
|
for partNumber <= totalPartsCount {
|
2015-12-28 20:23:53 +00:00
|
|
|
// Get a section reader on a particular offset.
|
2016-01-07 19:23:38 +00:00
|
|
|
sectionReader := io.NewSectionReader(fileReader, totalUploadedSize, partSize)
|
2015-12-28 20:23:53 +00:00
|
|
|
|
2016-07-29 18:28:44 +00:00
|
|
|
// Add hash algorithms that need to be calculated by computeHash()
|
|
|
|
// In case of a non-v4 signature or https connection, sha256 is not needed.
|
|
|
|
hashAlgos := make(map[string]hash.Hash)
|
|
|
|
hashSums := make(map[string][]byte)
|
|
|
|
hashAlgos["md5"] = md5.New()
|
|
|
|
if c.signature.isV4() && !c.secure {
|
|
|
|
hashAlgos["sha256"] = sha256.New()
|
|
|
|
}
|
|
|
|
|
2016-01-27 22:18:23 +00:00
|
|
|
var prtSize int64
|
2016-07-29 18:28:44 +00:00
|
|
|
prtSize, err = computeHash(hashAlgos, hashSums, sectionReader)
|
2015-12-28 20:23:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2016-01-27 22:18:23 +00:00
|
|
|
var reader io.Reader
|
|
|
|
// Update progress reader appropriately to the latest offset
|
|
|
|
// as we read from the source.
|
|
|
|
reader = newHook(sectionReader, progress)
|
|
|
|
|
|
|
|
// Verify if part should be uploaded.
|
|
|
|
if shouldUploadPart(objectPart{
|
2016-07-29 18:28:44 +00:00
|
|
|
ETag: hex.EncodeToString(hashSums["md5"]),
|
2016-01-07 19:23:38 +00:00
|
|
|
PartNumber: partNumber,
|
2016-01-27 22:18:23 +00:00
|
|
|
Size: prtSize,
|
2015-12-28 20:23:53 +00:00
|
|
|
}, partsInfo) {
|
2016-01-07 19:23:38 +00:00
|
|
|
// Proceed to upload the part.
|
2016-01-27 22:18:23 +00:00
|
|
|
var objPart objectPart
|
2016-04-18 19:27:13 +00:00
|
|
|
objPart, err = c.uploadPart(bucketName, objectName, uploadID, reader, partNumber,
|
2016-07-29 18:28:44 +00:00
|
|
|
hashSums["md5"], hashSums["sha256"], prtSize)
|
2016-01-02 13:37:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return totalUploadedSize, err
|
|
|
|
}
|
|
|
|
// Save successfully uploaded part metadata.
|
2016-01-07 19:23:38 +00:00
|
|
|
partsInfo[partNumber] = objPart
|
2016-01-27 22:18:23 +00:00
|
|
|
} else {
|
|
|
|
// Update the progress reader for the skipped part.
|
|
|
|
if progress != nil {
|
|
|
|
if _, err = io.CopyN(ioutil.Discard, progress, prtSize); err != nil {
|
|
|
|
return totalUploadedSize, err
|
|
|
|
}
|
|
|
|
}
|
2015-12-28 20:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save successfully uploaded size.
|
2016-01-27 22:18:23 +00:00
|
|
|
totalUploadedSize += prtSize
|
|
|
|
|
|
|
|
// Increment part number.
|
|
|
|
partNumber++
|
2015-12-28 20:23:53 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 19:23:38 +00:00
|
|
|
// Verify if we uploaded all data.
|
2015-12-28 20:23:53 +00:00
|
|
|
if totalUploadedSize != fileSize {
|
|
|
|
return totalUploadedSize, ErrUnexpectedEOF(totalUploadedSize, fileSize, bucketName, objectName)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over uploaded parts to save them in a Parts array before completing the multipart request.
|
|
|
|
for _, part := range partsInfo {
|
|
|
|
var complPart completePart
|
|
|
|
complPart.ETag = part.ETag
|
|
|
|
complPart.PartNumber = part.PartNumber
|
|
|
|
completeMultipartUpload.Parts = append(completeMultipartUpload.Parts, complPart)
|
|
|
|
}
|
|
|
|
|
2016-01-27 22:18:23 +00:00
|
|
|
// Verify if totalPartsCount is not equal to total list of parts.
|
|
|
|
if totalPartsCount != len(completeMultipartUpload.Parts) {
|
2016-01-02 13:37:08 +00:00
|
|
|
return totalUploadedSize, ErrInvalidParts(partNumber, len(completeMultipartUpload.Parts))
|
|
|
|
}
|
|
|
|
|
2015-12-28 20:23:53 +00:00
|
|
|
// Sort all completed parts.
|
|
|
|
sort.Sort(completedParts(completeMultipartUpload.Parts))
|
|
|
|
_, err = c.completeMultipartUpload(bucketName, objectName, uploadID, completeMultipartUpload)
|
|
|
|
if err != nil {
|
|
|
|
return totalUploadedSize, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return final size.
|
|
|
|
return totalUploadedSize, nil
|
|
|
|
}
|