xl/fs: Move few functions into common code. (#1453)

- PutObject()
- PutObjectPart()
- NewMultipartUpload()
- AbortMultipartUpload()

Implementations across both FS and XL object layer
share common implementation.
This commit is contained in:
Harshavardhana
2016-05-02 16:57:31 -07:00
committed by Anand Babu (AB) Periasamy
parent 3bf3d18f1f
commit afd59c45dc
6 changed files with 288 additions and 436 deletions

View File

@@ -17,8 +17,6 @@
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"io"
"path"
@@ -167,73 +165,9 @@ func (xl xlObjects) GetObjectInfo(bucket, object string) (ObjectInfo, error) {
}, nil
}
// PutObject - create an object.
func (xl xlObjects) PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (string, error) {
// Verify if bucket is valid.
if !IsValidBucketName(bucket) {
return "", (BucketNameInvalid{Bucket: bucket})
}
if !IsValidObjectName(object) {
return "", (ObjectNameInvalid{
Bucket: bucket,
Object: object,
})
}
// Check whether the bucket exists.
if isExist, err := isBucketExist(xl.storage, bucket); err != nil {
return "", err
} else if !isExist {
return "", BucketNotFound{Bucket: bucket}
}
fileWriter, err := xl.storage.CreateFile(bucket, object)
if err != nil {
return "", toObjectErr(err, bucket, object)
}
// Initialize md5 writer.
md5Writer := md5.New()
// Instantiate a new multi writer.
multiWriter := io.MultiWriter(md5Writer, fileWriter)
// Instantiate checksum hashers and create a multiwriter.
if size > 0 {
if _, err = io.CopyN(multiWriter, data, size); err != nil {
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
return "", clErr
}
return "", toObjectErr(err)
}
} else {
if _, err = io.Copy(multiWriter, data); err != nil {
if clErr := safeCloseAndRemove(fileWriter); clErr != nil {
return "", clErr
}
return "", err
}
}
newMD5Hex := hex.EncodeToString(md5Writer.Sum(nil))
// md5Hex representation.
var md5Hex string
if len(metadata) != 0 {
md5Hex = metadata["md5Sum"]
}
if md5Hex != "" {
if newMD5Hex != md5Hex {
if err = safeCloseAndRemove(fileWriter); err != nil {
return "", err
}
return "", BadDigest{md5Hex, newMD5Hex}
}
}
err = fileWriter.Close()
if err != nil {
return "", err
}
// Return md5sum, successfully wrote object.
return newMD5Hex, nil
return putObjectCommon(xl.storage, bucket, object, size, data, metadata)
}
func (xl xlObjects) DeleteObject(bucket, object string) error {