2015-10-16 14:26:01 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2015 Minio, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
package main
|
2015-10-16 14:26:01 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-03-12 19:08:15 -05:00
|
|
|
"crypto/md5"
|
2015-10-16 14:26:01 -04:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"encoding/hex"
|
|
|
|
"runtime"
|
|
|
|
|
2016-02-10 19:40:09 -05:00
|
|
|
"github.com/minio/minio/pkg/atomic"
|
2015-10-17 22:17:33 -04:00
|
|
|
"github.com/minio/minio/pkg/disk"
|
2016-02-05 18:09:23 -05:00
|
|
|
"github.com/minio/minio/pkg/mimedb"
|
2016-02-10 19:40:09 -05:00
|
|
|
"github.com/minio/minio/pkg/probe"
|
2015-10-16 14:26:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
/// Object Operations
|
|
|
|
|
|
|
|
// GetObject - GET object
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
func (fs Filesystem) GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error) {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Input validation.
|
2015-12-07 16:39:18 -05:00
|
|
|
if !IsValidBucketName(bucket) {
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
return nil, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
return nil, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// normalize buckets.
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
bucket = getActualBucketname(fs.path, bucket)
|
2016-03-13 04:58:07 -04:00
|
|
|
objectPath := filepath.Join(fs.path, bucket, object)
|
|
|
|
|
|
|
|
file, e := os.Open(objectPath)
|
|
|
|
if e != nil {
|
|
|
|
// If the object doesn't exist, the bucket might not exist either. Stat for
|
|
|
|
// the bucket and give a better error message if that is true.
|
2016-01-25 02:03:38 -05:00
|
|
|
if os.IsNotExist(e) {
|
2016-03-13 04:58:07 -04:00
|
|
|
_, e = os.Stat(filepath.Join(fs.path, bucket))
|
|
|
|
if os.IsNotExist(e) {
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
return nil, probe.NewError(BucketNotFound{Bucket: bucket})
|
2016-03-13 04:58:07 -04:00
|
|
|
}
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
return nil, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
2016-01-25 02:03:38 -05:00
|
|
|
}
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
return nil, probe.NewError(e)
|
2016-01-25 02:03:38 -05:00
|
|
|
}
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
// Initiate a cached stat operation on the file handler.
|
|
|
|
st, e := file.Stat()
|
2016-03-13 04:58:07 -04:00
|
|
|
if e != nil {
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
return nil, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
// Object path is a directory prefix, return object not found error.
|
|
|
|
if st.IsDir() {
|
|
|
|
return nil, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
2016-03-13 04:58:07 -04:00
|
|
|
}
|
|
|
|
|
objectAPI: Fix object API interface, remove unnecessary structs.
ObjectAPI changes.
```
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsInfo, *probe.Error)
ListMultipartUploads(bucket, objectPrefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int) (ListMultipartsInfo, *probe.Error)
ListObjectParts(bucket, object, uploadID string, partNumberMarker, maxParts int) (ListPartsInfo, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []completePart) (ObjectInfo, *probe.Error)
```
2016-04-03 04:34:20 -04:00
|
|
|
// Seek to a starting offset.
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
_, e = file.Seek(startOffset, os.SEEK_SET)
|
2016-03-13 04:58:07 -04:00
|
|
|
if e != nil {
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
// When the "handle is invalid", the file might be a directory on Windows.
|
|
|
|
if runtime.GOOS == "windows" && strings.Contains(e.Error(), "handle is invalid") {
|
|
|
|
return nil, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
return nil, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
return file, nil
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-11 19:31:24 -05:00
|
|
|
// GetObjectInfo - get object info.
|
|
|
|
func (fs Filesystem) GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error) {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Input validation.
|
2015-12-07 16:39:18 -05:00
|
|
|
if !IsValidBucketName(bucket) {
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if !IsValidObjectName(object) {
|
2016-03-23 09:50:24 -04:00
|
|
|
return ObjectInfo{}, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Normalize buckets.
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
bucket = getActualBucketname(fs.path, bucket)
|
2016-01-25 20:29:20 -05:00
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
|
|
|
if _, e := os.Stat(bucketPath); e != nil {
|
|
|
|
if os.IsNotExist(e) {
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
2016-01-25 20:29:20 -05:00
|
|
|
}
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2016-01-25 20:29:20 -05:00
|
|
|
}
|
|
|
|
|
2016-03-11 19:31:24 -05:00
|
|
|
info, err := getObjectInfo(fs.path, bucket, object)
|
2015-10-16 14:26:01 -04:00
|
|
|
if err != nil {
|
2016-03-23 09:50:24 -04:00
|
|
|
if os.IsNotExist(err.ToGoError()) {
|
|
|
|
return ObjectInfo{}, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, err.Trace(bucket, object)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-03-23 09:50:24 -04:00
|
|
|
if info.IsDir {
|
|
|
|
return ObjectInfo{}, probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-03-11 19:31:24 -05:00
|
|
|
return info, nil
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-03-11 19:31:24 -05:00
|
|
|
// getObjectInfo - get object stat info.
|
|
|
|
func getObjectInfo(rootPath, bucket, object string) (ObjectInfo, *probe.Error) {
|
2016-02-04 23:40:58 -05:00
|
|
|
// Do not use filepath.Join() since filepath.Join strips off any
|
|
|
|
// object names with '/', use them as is in a static manner so
|
2016-03-23 09:50:24 -04:00
|
|
|
// that we can send a proper 'ObjectNotFound' reply back upon os.Stat().
|
2015-10-16 14:26:01 -04:00
|
|
|
var objectPath string
|
2015-10-22 01:25:13 -04:00
|
|
|
// For windows use its special os.PathSeparator == "\\"
|
2015-10-16 14:26:01 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
2015-10-17 15:01:19 -04:00
|
|
|
objectPath = rootPath + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
2015-10-16 14:26:01 -04:00
|
|
|
} else {
|
2015-10-17 15:01:19 -04:00
|
|
|
objectPath = rootPath + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-03-23 09:50:24 -04:00
|
|
|
stat, e := os.Stat(objectPath)
|
|
|
|
if e != nil {
|
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
contentType := "application/octet-stream"
|
2015-10-22 01:25:13 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
2016-02-19 19:04:29 -05:00
|
|
|
object = filepath.ToSlash(object)
|
2015-10-22 01:25:13 -04:00
|
|
|
}
|
2016-02-05 18:09:23 -05:00
|
|
|
|
2016-02-01 15:19:54 -05:00
|
|
|
if objectExt := filepath.Ext(object); objectExt != "" {
|
2016-02-05 18:09:23 -05:00
|
|
|
content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]
|
|
|
|
if ok {
|
|
|
|
contentType = content.ContentType
|
|
|
|
}
|
2016-02-01 15:19:54 -05:00
|
|
|
}
|
2016-03-11 19:31:24 -05:00
|
|
|
metadata := ObjectInfo{
|
2016-02-27 06:04:52 -05:00
|
|
|
Bucket: bucket,
|
2016-03-11 19:31:24 -05:00
|
|
|
Name: object,
|
|
|
|
ModifiedTime: stat.ModTime(),
|
2016-02-27 06:04:52 -05:00
|
|
|
Size: stat.Size(),
|
|
|
|
ContentType: contentType,
|
2016-03-11 19:31:24 -05:00
|
|
|
IsDir: stat.Mode().IsDir(),
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
return metadata, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isMD5SumEqual - returns error if md5sum mismatches, success its `nil`
|
2016-02-04 15:52:25 -05:00
|
|
|
func isMD5SumEqual(expectedMD5Sum, actualMD5Sum string) bool {
|
2016-02-06 04:36:43 -05:00
|
|
|
// Verify the md5sum.
|
2016-02-15 20:42:39 -05:00
|
|
|
if expectedMD5Sum != "" && actualMD5Sum != "" {
|
2016-02-06 04:36:43 -05:00
|
|
|
// Decode md5sum to bytes from their hexadecimal
|
|
|
|
// representations.
|
2015-10-16 14:26:01 -04:00
|
|
|
expectedMD5SumBytes, err := hex.DecodeString(expectedMD5Sum)
|
|
|
|
if err != nil {
|
2016-02-04 15:52:25 -05:00
|
|
|
return false
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
actualMD5SumBytes, err := hex.DecodeString(actualMD5Sum)
|
|
|
|
if err != nil {
|
2016-02-04 15:52:25 -05:00
|
|
|
return false
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-06 04:36:43 -05:00
|
|
|
// Verify md5sum bytes are equal after successful decoding.
|
2015-10-16 14:26:01 -04:00
|
|
|
if !bytes.Equal(expectedMD5SumBytes, actualMD5SumBytes) {
|
2016-02-04 15:52:25 -05:00
|
|
|
return false
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-04 15:52:25 -05:00
|
|
|
return true
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-04 15:52:25 -05:00
|
|
|
return false
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
// PutObject - create an object.
|
|
|
|
func (fs Filesystem) PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error) {
|
2016-02-02 18:23:28 -05:00
|
|
|
di, e := disk.GetInfo(fs.path)
|
|
|
|
if e != nil {
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-17 22:17:33 -04:00
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Remove 5% from total space for cumulative disk space used for
|
|
|
|
// journalling, inodes etc.
|
2016-01-25 02:03:38 -05:00
|
|
|
availableDiskSpace := (float64(di.Free) / (float64(di.Total) - (0.05 * float64(di.Total)))) * 100
|
2015-10-19 13:29:54 -04:00
|
|
|
if int64(availableDiskSpace) <= fs.minFreeDisk {
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(RootPathFull{Path: fs.path})
|
2015-10-17 22:17:33 -04:00
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Check bucket name valid.
|
2015-12-07 16:39:18 -05:00
|
|
|
if !IsValidBucketName(bucket) {
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(BucketNameInvalid{Bucket: bucket})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-01-25 02:03:38 -05:00
|
|
|
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
bucket = getActualBucketname(fs.path, bucket)
|
2016-01-25 02:03:38 -05:00
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
2016-02-02 18:23:28 -05:00
|
|
|
if _, e = os.Stat(bucketPath); e != nil {
|
2016-01-25 02:03:38 -05:00
|
|
|
if os.IsNotExist(e) {
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(BucketNotFound{Bucket: bucket})
|
2016-01-25 02:03:38 -05:00
|
|
|
}
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-06 04:36:43 -05:00
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Verify object path legal.
|
2015-10-16 14:26:01 -04:00
|
|
|
if !IsValidObjectName(object) {
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Get object path.
|
2016-01-25 02:03:38 -05:00
|
|
|
objectPath := filepath.Join(bucketPath, object)
|
2016-03-12 19:08:15 -05:00
|
|
|
|
|
|
|
// md5Hex representation.
|
|
|
|
var md5Hex string
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
if len(metadata) != 0 {
|
|
|
|
md5Hex = metadata["md5Sum"]
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// Write object.
|
2016-03-12 19:08:15 -05:00
|
|
|
file, e := atomic.FileCreateWithPrefix(objectPath, md5Hex+"$tmpobject")
|
2016-02-02 18:23:28 -05:00
|
|
|
if e != nil {
|
|
|
|
switch e := e.(type) {
|
|
|
|
case *os.PathError:
|
|
|
|
if e.Op == "mkdir" {
|
|
|
|
if strings.Contains(e.Error(), "not a directory") {
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(ObjectExistsAsPrefix{Bucket: bucket, Prefix: object})
|
2016-02-02 18:23:28 -05:00
|
|
|
}
|
|
|
|
}
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2016-02-02 18:23:28 -05:00
|
|
|
default:
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2016-02-02 18:23:28 -05:00
|
|
|
}
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-03-12 19:08:15 -05:00
|
|
|
defer file.Close()
|
2015-10-16 14:26:01 -04:00
|
|
|
|
2016-03-12 19:08:15 -05:00
|
|
|
// Initialize md5 writer.
|
|
|
|
md5Writer := md5.New()
|
2015-10-16 14:26:01 -04:00
|
|
|
|
2016-03-12 19:08:15 -05:00
|
|
|
// Instantiate a new multi writer.
|
|
|
|
multiWriter := io.MultiWriter(md5Writer, file)
|
|
|
|
|
|
|
|
// Instantiate checksum hashers and create a multiwriter.
|
2015-10-16 14:26:01 -04:00
|
|
|
if size > 0 {
|
2016-03-12 19:08:15 -05:00
|
|
|
if _, e = io.CopyN(multiWriter, data, size); e != nil {
|
2015-10-16 14:26:01 -04:00
|
|
|
file.CloseAndPurge()
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
} else {
|
2016-03-12 19:08:15 -05:00
|
|
|
if _, e = io.Copy(multiWriter, data); e != nil {
|
2015-10-16 14:26:01 -04:00
|
|
|
file.CloseAndPurge()
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 19:08:15 -05:00
|
|
|
newMD5Hex := hex.EncodeToString(md5Writer.Sum(nil))
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
if md5Hex != "" {
|
2016-03-12 19:08:15 -05:00
|
|
|
if newMD5Hex != md5Hex {
|
|
|
|
return ObjectInfo{}, probe.NewError(BadDigest{md5Hex, newMD5Hex})
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Set stat again to get the latest metadata.
|
2016-03-12 19:08:15 -05:00
|
|
|
st, e := os.Stat(file.Name())
|
2016-02-02 18:23:28 -05:00
|
|
|
if e != nil {
|
2016-03-11 19:31:24 -05:00
|
|
|
return ObjectInfo{}, probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-03-12 19:08:15 -05:00
|
|
|
|
2016-02-01 15:19:54 -05:00
|
|
|
contentType := "application/octet-stream"
|
|
|
|
if objectExt := filepath.Ext(objectPath); objectExt != "" {
|
2016-02-05 18:09:23 -05:00
|
|
|
content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]
|
|
|
|
if ok {
|
|
|
|
contentType = content.ContentType
|
|
|
|
}
|
2016-02-01 15:19:54 -05:00
|
|
|
}
|
2016-03-11 19:31:24 -05:00
|
|
|
newObject := ObjectInfo{
|
2016-02-27 06:04:52 -05:00
|
|
|
Bucket: bucket,
|
2016-03-11 19:31:24 -05:00
|
|
|
Name: object,
|
|
|
|
ModifiedTime: st.ModTime(),
|
2016-02-27 06:04:52 -05:00
|
|
|
Size: st.Size(),
|
2016-03-12 19:08:15 -05:00
|
|
|
MD5Sum: newMD5Hex,
|
2016-02-27 06:04:52 -05:00
|
|
|
ContentType: contentType,
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
return newObject, nil
|
|
|
|
}
|
|
|
|
|
2016-02-04 23:40:58 -05:00
|
|
|
// deleteObjectPath - delete object path if its empty.
|
2015-10-29 18:09:29 -04:00
|
|
|
func deleteObjectPath(basePath, deletePath, bucket, object string) *probe.Error {
|
|
|
|
if basePath == deletePath {
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-06 04:36:43 -05:00
|
|
|
// Verify if the path exists.
|
2016-02-04 23:40:58 -05:00
|
|
|
pathSt, e := os.Stat(deletePath)
|
2016-02-02 18:23:28 -05:00
|
|
|
if e != nil {
|
|
|
|
if os.IsNotExist(e) {
|
2015-10-29 18:09:29 -04:00
|
|
|
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-02-02 18:23:28 -05:00
|
|
|
return probe.NewError(e)
|
2015-10-29 18:09:29 -04:00
|
|
|
}
|
2016-02-04 23:40:58 -05:00
|
|
|
if pathSt.IsDir() {
|
2016-02-06 04:36:43 -05:00
|
|
|
// Verify if directory is empty.
|
2016-03-11 22:47:44 -05:00
|
|
|
empty, e := isDirEmpty(deletePath)
|
2016-02-04 15:52:25 -05:00
|
|
|
if e != nil {
|
|
|
|
return probe.NewError(e)
|
2015-10-29 18:09:29 -04:00
|
|
|
}
|
|
|
|
if !empty {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2016-02-06 04:36:43 -05:00
|
|
|
// Attempt to remove path.
|
2016-02-02 18:23:28 -05:00
|
|
|
if e := os.Remove(deletePath); e != nil {
|
|
|
|
return probe.NewError(e)
|
2015-10-29 18:09:29 -04:00
|
|
|
}
|
2016-02-06 04:36:43 -05:00
|
|
|
// Recursively go down the next path and delete again.
|
2015-10-29 18:09:29 -04:00
|
|
|
if err := deleteObjectPath(basePath, filepath.Dir(deletePath), bucket, object); err != nil {
|
2016-02-02 18:23:28 -05:00
|
|
|
return err.Trace(basePath, deletePath, bucket, object)
|
2015-10-29 18:09:29 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// DeleteObject - delete object.
|
2015-10-17 22:17:33 -04:00
|
|
|
func (fs Filesystem) DeleteObject(bucket, object string) *probe.Error {
|
2016-02-06 04:36:43 -05:00
|
|
|
// Check bucket name valid
|
2015-12-07 16:39:18 -05:00
|
|
|
if !IsValidBucketName(bucket) {
|
2015-10-16 14:26:01 -04:00
|
|
|
return probe.NewError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
|
fs: Break fs package to top-level and introduce ObjectAPI interface.
ObjectAPI interface brings in changes needed for XL ObjectAPI layer.
The new interface for any ObjectAPI layer is as below
```
// ObjectAPI interface.
type ObjectAPI interface {
// Bucket resource API.
DeleteBucket(bucket string) *probe.Error
ListBuckets() ([]BucketInfo, *probe.Error)
MakeBucket(bucket string) *probe.Error
GetBucketInfo(bucket string) (BucketInfo, *probe.Error)
// Bucket query API.
ListObjects(bucket, prefix, marker, delimiter string, maxKeys int) (ListObjectsResult, *probe.Error)
ListMultipartUploads(bucket string, resources BucketMultipartResourcesMetadata) (BucketMultipartResourcesMetadata, *probe.Error)
// Object resource API.
GetObject(bucket, object string, startOffset int64) (io.ReadCloser, *probe.Error)
GetObjectInfo(bucket, object string) (ObjectInfo, *probe.Error)
PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (ObjectInfo, *probe.Error)
DeleteObject(bucket, object string) *probe.Error
// Object query API.
NewMultipartUpload(bucket, object string) (string, *probe.Error)
PutObjectPart(bucket, object, uploadID string, partID int, size int64, data io.Reader, md5Hex string) (string, *probe.Error)
ListObjectParts(bucket, object string, resources ObjectResourcesMetadata) (ObjectResourcesMetadata, *probe.Error)
CompleteMultipartUpload(bucket string, object string, uploadID string, parts []CompletePart) (ObjectInfo, *probe.Error)
AbortMultipartUpload(bucket, object, uploadID string) *probe.Error
}
```
2016-03-30 19:15:28 -04:00
|
|
|
bucket = getActualBucketname(fs.path, bucket)
|
2015-10-29 18:09:29 -04:00
|
|
|
bucketPath := filepath.Join(fs.path, bucket)
|
2016-02-06 04:36:43 -05:00
|
|
|
// Check bucket exists
|
2016-01-25 02:03:38 -05:00
|
|
|
if _, e := os.Stat(bucketPath); e != nil {
|
|
|
|
if os.IsNotExist(e) {
|
|
|
|
return probe.NewError(BucketNotFound{Bucket: bucket})
|
|
|
|
}
|
|
|
|
return probe.NewError(e)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Verify object path legal
|
2015-10-16 14:26:01 -04:00
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return probe.NewError(ObjectNameInvalid{Bucket: bucket, Object: object})
|
|
|
|
}
|
|
|
|
|
2016-02-06 04:36:43 -05:00
|
|
|
// Do not use filepath.Join() since filepath.Join strips off any
|
|
|
|
// object names with '/', use them as is in a static manner so
|
|
|
|
// that we can send a proper 'ObjectNotFound' reply back upon
|
|
|
|
// os.Stat().
|
2015-10-16 14:26:01 -04:00
|
|
|
var objectPath string
|
|
|
|
if runtime.GOOS == "windows" {
|
2015-10-17 15:01:19 -04:00
|
|
|
objectPath = fs.path + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
2015-10-16 14:26:01 -04:00
|
|
|
} else {
|
2015-10-17 15:01:19 -04:00
|
|
|
objectPath = fs.path + string(os.PathSeparator) + bucket + string(os.PathSeparator) + object
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
2016-02-06 04:36:43 -05:00
|
|
|
// Delete object path if its empty.
|
2015-10-29 18:09:29 -04:00
|
|
|
err := deleteObjectPath(bucketPath, objectPath, bucket, object)
|
|
|
|
if err != nil {
|
2016-02-04 23:40:58 -05:00
|
|
|
if os.IsNotExist(err.ToGoError()) {
|
|
|
|
return probe.NewError(ObjectNotFound{Bucket: bucket, Object: object})
|
|
|
|
}
|
2016-02-02 18:23:28 -05:00
|
|
|
return err.Trace(bucketPath, objectPath, bucket, object)
|
2015-10-16 14:26:01 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|