2017-04-27 14:26:00 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2017 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2017-09-05 17:45:22 -04:00
|
|
|
"strings"
|
2017-04-27 14:26:00 -04:00
|
|
|
|
2017-05-12 01:38:22 -04:00
|
|
|
"encoding/hex"
|
|
|
|
|
2017-04-27 14:26:00 -04:00
|
|
|
minio "github.com/minio/minio-go"
|
|
|
|
"github.com/minio/minio-go/pkg/policy"
|
2017-09-28 22:37:09 -04:00
|
|
|
"github.com/minio/minio-go/pkg/s3utils"
|
2017-04-27 14:26:00 -04:00
|
|
|
)
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// s3ToObjectError converts Minio errors to minio object layer errors.
|
2017-04-27 14:26:00 -04:00
|
|
|
func s3ToObjectError(err error, params ...string) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
e, ok := err.(*Error)
|
|
|
|
if !ok {
|
|
|
|
// Code should be fixed if this function is called without doing traceError()
|
|
|
|
// Else handling different situations in this function makes this function complicated.
|
|
|
|
errorIf(err, "Expected type *Error")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = e.e
|
|
|
|
|
|
|
|
bucket := ""
|
|
|
|
object := ""
|
|
|
|
if len(params) >= 1 {
|
|
|
|
bucket = params[0]
|
|
|
|
}
|
|
|
|
if len(params) == 2 {
|
|
|
|
object = params[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
minioErr, ok := err.(minio.ErrorResponse)
|
|
|
|
if !ok {
|
|
|
|
// We don't interpret non Minio errors. As minio errors will
|
|
|
|
// have StatusCode to help to convert to object errors.
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
switch minioErr.Code {
|
|
|
|
case "BucketAlreadyOwnedByYou":
|
|
|
|
err = BucketAlreadyOwnedByYou{}
|
|
|
|
case "BucketNotEmpty":
|
|
|
|
err = BucketNotEmpty{}
|
|
|
|
case "NoSuchBucketPolicy":
|
|
|
|
err = PolicyNotFound{}
|
|
|
|
case "InvalidBucketName":
|
|
|
|
err = BucketNameInvalid{Bucket: bucket}
|
|
|
|
case "NoSuchBucket":
|
|
|
|
err = BucketNotFound{Bucket: bucket}
|
|
|
|
case "NoSuchKey":
|
|
|
|
if object != "" {
|
|
|
|
err = ObjectNotFound{Bucket: bucket, Object: object}
|
|
|
|
} else {
|
|
|
|
err = BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
case "XMinioInvalidObjectName":
|
|
|
|
err = ObjectNameInvalid{}
|
|
|
|
case "AccessDenied":
|
|
|
|
err = PrefixAccessDenied{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
}
|
|
|
|
case "XAmzContentSHA256Mismatch":
|
|
|
|
err = SHA256Mismatch{}
|
|
|
|
}
|
|
|
|
|
|
|
|
e.e = err
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2017-05-15 03:52:33 -04:00
|
|
|
// s3Objects implements gateway for Minio and S3 compatible object storage servers.
|
|
|
|
type s3Objects struct {
|
2017-04-27 14:26:00 -04:00
|
|
|
Client *minio.Core
|
|
|
|
anonClient *minio.Core
|
|
|
|
}
|
|
|
|
|
|
|
|
// newS3Gateway returns s3 gatewaylayer
|
2017-06-09 22:50:51 -04:00
|
|
|
func newS3Gateway(host string) (GatewayLayer, error) {
|
2017-05-12 01:38:22 -04:00
|
|
|
|
|
|
|
var err error
|
2017-06-09 22:50:51 -04:00
|
|
|
var endpoint string
|
|
|
|
var secure = true
|
2017-05-08 18:42:48 -04:00
|
|
|
|
2017-06-09 22:50:51 -04:00
|
|
|
// Validate host parameters.
|
|
|
|
if host != "" {
|
|
|
|
// Override default params if the host is provided
|
|
|
|
endpoint, secure, err = parseGatewayEndpoint(host)
|
2017-05-12 01:38:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-04 01:41:03 -04:00
|
|
|
}
|
|
|
|
|
2017-06-09 22:50:51 -04:00
|
|
|
// Default endpoint parameters
|
2017-05-04 01:41:03 -04:00
|
|
|
if endpoint == "" {
|
|
|
|
endpoint = "s3.amazonaws.com"
|
|
|
|
}
|
|
|
|
|
2017-06-09 22:50:51 -04:00
|
|
|
creds := serverConfig.GetCredential()
|
2017-05-08 18:42:48 -04:00
|
|
|
|
2017-04-27 14:26:00 -04:00
|
|
|
// Initialize minio client object.
|
2017-06-09 22:50:51 -04:00
|
|
|
client, err := minio.NewCore(endpoint, creds.AccessKey, creds.SecretKey, secure)
|
2017-04-27 14:26:00 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
anonClient, err := minio.NewCore(endpoint, "", "", secure)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-03 21:38:27 -04:00
|
|
|
anonClient.SetCustomTransport(newCustomHTTPTransport())
|
2017-04-27 14:26:00 -04:00
|
|
|
|
2017-05-15 03:52:33 -04:00
|
|
|
return &s3Objects{
|
2017-04-27 14:26:00 -04:00
|
|
|
Client: client,
|
|
|
|
anonClient: anonClient,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// Shutdown saves any gateway metadata to disk
|
2017-04-27 14:26:00 -04:00
|
|
|
// if necessary and reload upon next restart.
|
2017-05-15 03:52:33 -04:00
|
|
|
func (l *s3Objects) Shutdown() error {
|
2017-04-27 14:26:00 -04:00
|
|
|
// TODO
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// StorageInfo is not relevant to S3 backend.
|
2017-06-21 22:53:09 -04:00
|
|
|
func (l *s3Objects) StorageInfo() (si StorageInfo) {
|
|
|
|
return si
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// MakeBucket creates a new container on S3 backend.
|
2017-05-15 03:52:33 -04:00
|
|
|
func (l *s3Objects) MakeBucketWithLocation(bucket, location string) error {
|
2017-04-27 14:26:00 -04:00
|
|
|
err := l.Client.MakeBucket(bucket, location)
|
|
|
|
if err != nil {
|
|
|
|
return s3ToObjectError(traceError(err), bucket)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// GetBucketInfo gets bucket metadata..
|
2017-06-21 22:53:09 -04:00
|
|
|
func (l *s3Objects) GetBucketInfo(bucket string) (bi BucketInfo, e error) {
|
2017-09-28 22:37:09 -04:00
|
|
|
// Verify if bucket name is valid.
|
|
|
|
// We are using a separate helper function here to validate bucket
|
|
|
|
// names instead of IsValidBucketName() because there is a possibility
|
|
|
|
// that certains users might have buckets which are non-DNS compliant
|
|
|
|
// in us-east-1 and we might severely restrict them by not allowing
|
|
|
|
// access to these buckets.
|
|
|
|
// Ref - http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html
|
|
|
|
if s3utils.CheckValidBucketName(bucket) != nil {
|
|
|
|
return bi, traceError(BucketNameInvalid{Bucket: bucket})
|
|
|
|
}
|
|
|
|
|
2017-04-27 14:26:00 -04:00
|
|
|
buckets, err := l.Client.ListBuckets()
|
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return bi, s3ToObjectError(traceError(err), bucket)
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, bi := range buckets {
|
|
|
|
if bi.Name != bucket {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return BucketInfo{
|
|
|
|
Name: bi.Name,
|
|
|
|
Created: bi.CreationDate,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-06-21 22:53:09 -04:00
|
|
|
return bi, traceError(BucketNotFound{Bucket: bucket})
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// ListBuckets lists all S3 buckets
|
2017-05-15 03:52:33 -04:00
|
|
|
func (l *s3Objects) ListBuckets() ([]BucketInfo, error) {
|
2017-04-27 14:26:00 -04:00
|
|
|
buckets, err := l.Client.ListBuckets()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := make([]BucketInfo, len(buckets))
|
|
|
|
for i, bi := range buckets {
|
|
|
|
b[i] = BucketInfo{
|
|
|
|
Name: bi.Name,
|
|
|
|
Created: bi.CreationDate,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, err
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// DeleteBucket deletes a bucket on S3
|
2017-05-15 03:52:33 -04:00
|
|
|
func (l *s3Objects) DeleteBucket(bucket string) error {
|
2017-04-27 14:26:00 -04:00
|
|
|
err := l.Client.RemoveBucket(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return s3ToObjectError(traceError(err), bucket)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// ListObjects lists all blobs in S3 bucket filtered by prefix
|
2017-06-21 22:53:09 -04:00
|
|
|
func (l *s3Objects) ListObjects(bucket string, prefix string, marker string, delimiter string, maxKeys int) (loi ListObjectsInfo, e error) {
|
2017-04-27 14:26:00 -04:00
|
|
|
result, err := l.Client.ListObjects(bucket, prefix, marker, delimiter, maxKeys)
|
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return loi, s3ToObjectError(traceError(err), bucket)
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return fromMinioClientListBucketResult(bucket, result), nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// ListObjectsV2 lists all blobs in S3 bucket filtered by prefix
|
2017-09-29 15:08:23 -04:00
|
|
|
func (l *s3Objects) ListObjectsV2(bucket, prefix, continuationToken, delimiter string, maxKeys int, fetchOwner bool, startAfter string) (loi ListObjectsV2Info, e error) {
|
2017-04-27 14:26:00 -04:00
|
|
|
result, err := l.Client.ListObjectsV2(bucket, prefix, continuationToken, fetchOwner, delimiter, maxKeys)
|
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return loi, s3ToObjectError(traceError(err), bucket)
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return fromMinioClientListBucketV2Result(bucket, result), nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// fromMinioClientListBucketV2Result converts minio ListBucketResult to ListObjectsInfo
|
2017-04-27 14:26:00 -04:00
|
|
|
func fromMinioClientListBucketV2Result(bucket string, result minio.ListBucketV2Result) ListObjectsV2Info {
|
|
|
|
objects := make([]ObjectInfo, len(result.Contents))
|
|
|
|
|
|
|
|
for i, oi := range result.Contents {
|
|
|
|
objects[i] = fromMinioClientObjectInfo(bucket, oi)
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixes := make([]string, len(result.CommonPrefixes))
|
|
|
|
for i, p := range result.CommonPrefixes {
|
|
|
|
prefixes[i] = p.Prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
return ListObjectsV2Info{
|
|
|
|
IsTruncated: result.IsTruncated,
|
|
|
|
Prefixes: prefixes,
|
|
|
|
Objects: objects,
|
|
|
|
|
|
|
|
ContinuationToken: result.ContinuationToken,
|
|
|
|
NextContinuationToken: result.NextContinuationToken,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// fromMinioClientListBucketResult converts minio ListBucketResult to ListObjectsInfo
|
2017-04-27 14:26:00 -04:00
|
|
|
func fromMinioClientListBucketResult(bucket string, result minio.ListBucketResult) ListObjectsInfo {
|
|
|
|
objects := make([]ObjectInfo, len(result.Contents))
|
|
|
|
|
|
|
|
for i, oi := range result.Contents {
|
|
|
|
objects[i] = fromMinioClientObjectInfo(bucket, oi)
|
|
|
|
}
|
|
|
|
|
|
|
|
prefixes := make([]string, len(result.CommonPrefixes))
|
|
|
|
for i, p := range result.CommonPrefixes {
|
|
|
|
prefixes[i] = p.Prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
return ListObjectsInfo{
|
|
|
|
IsTruncated: result.IsTruncated,
|
|
|
|
NextMarker: result.NextMarker,
|
|
|
|
Prefixes: prefixes,
|
|
|
|
Objects: objects,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// GetObject reads an object from S3. Supports additional
|
2017-04-27 14:26:00 -04:00
|
|
|
// parameters like offset and length which are synonymous with
|
|
|
|
// HTTP Range requests.
|
|
|
|
//
|
|
|
|
// startOffset indicates the starting read location of the object.
|
|
|
|
// length indicates the total length of the object.
|
2017-05-15 03:52:33 -04:00
|
|
|
func (l *s3Objects) GetObject(bucket string, key string, startOffset int64, length int64, writer io.Writer) error {
|
2017-05-02 21:46:39 -04:00
|
|
|
r := minio.NewGetReqHeaders()
|
2017-06-06 21:19:35 -04:00
|
|
|
|
|
|
|
if length < 0 && length != -1 {
|
|
|
|
return s3ToObjectError(traceError(errInvalidArgument), bucket, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
if startOffset >= 0 && length >= 0 {
|
|
|
|
if err := r.SetRange(startOffset, startOffset+length-1); err != nil {
|
|
|
|
return s3ToObjectError(traceError(err), bucket, key)
|
|
|
|
}
|
2017-05-02 21:46:39 -04:00
|
|
|
}
|
|
|
|
object, _, err := l.Client.GetObject(bucket, key, r)
|
2017-04-27 14:26:00 -04:00
|
|
|
if err != nil {
|
|
|
|
return s3ToObjectError(traceError(err), bucket, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer object.Close()
|
|
|
|
|
2017-06-06 21:19:35 -04:00
|
|
|
if _, err := io.Copy(writer, object); err != nil {
|
2017-04-27 14:26:00 -04:00
|
|
|
return s3ToObjectError(traceError(err), bucket, key)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// fromMinioClientObjectInfo converts minio ObjectInfo to gateway ObjectInfo
|
2017-04-27 14:26:00 -04:00
|
|
|
func fromMinioClientObjectInfo(bucket string, oi minio.ObjectInfo) ObjectInfo {
|
|
|
|
userDefined := fromMinioClientMetadata(oi.Metadata)
|
|
|
|
userDefined["Content-Type"] = oi.ContentType
|
|
|
|
|
|
|
|
return ObjectInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Name: oi.Key,
|
|
|
|
ModTime: oi.LastModified,
|
|
|
|
Size: oi.Size,
|
2017-06-23 15:19:10 -04:00
|
|
|
ETag: canonicalizeETag(oi.ETag),
|
2017-04-27 14:26:00 -04:00
|
|
|
UserDefined: userDefined,
|
|
|
|
ContentType: oi.ContentType,
|
|
|
|
ContentEncoding: oi.Metadata.Get("Content-Encoding"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// GetObjectInfo reads object info and replies back ObjectInfo
|
2017-05-15 03:52:33 -04:00
|
|
|
func (l *s3Objects) GetObjectInfo(bucket string, object string) (objInfo ObjectInfo, err error) {
|
2017-05-02 21:46:39 -04:00
|
|
|
r := minio.NewHeadReqHeaders()
|
|
|
|
oi, err := l.Client.StatObject(bucket, object, r)
|
2017-04-27 14:26:00 -04:00
|
|
|
if err != nil {
|
|
|
|
return ObjectInfo{}, s3ToObjectError(traceError(err), bucket, object)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fromMinioClientObjectInfo(bucket, oi), nil
|
|
|
|
}
|
|
|
|
|
2017-09-05 17:45:22 -04:00
|
|
|
// PutObject creates a new object with the incoming data,
|
2017-09-19 15:40:27 -04:00
|
|
|
func (l *s3Objects) PutObject(bucket string, object string, data *HashReader, metadata map[string]string) (objInfo ObjectInfo, err error) {
|
|
|
|
sha256sumBytes, err := hex.DecodeString(data.sha256Sum)
|
|
|
|
if err != nil {
|
|
|
|
return objInfo, s3ToObjectError(traceError(err), bucket, object)
|
|
|
|
}
|
|
|
|
md5sumBytes, err := hex.DecodeString(metadata["etag"])
|
2017-09-05 17:45:22 -04:00
|
|
|
if err != nil {
|
|
|
|
return objInfo, s3ToObjectError(traceError(err), bucket, object)
|
|
|
|
}
|
|
|
|
delete(metadata, "etag")
|
2017-09-28 11:10:38 -04:00
|
|
|
oi, err := l.Client.PutObject(bucket, object, data, data.Size(), md5sumBytes, sha256sumBytes, toMinioClientMetadata(metadata))
|
2017-04-27 14:26:00 -04:00
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return objInfo, s3ToObjectError(traceError(err), bucket, object)
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return fromMinioClientObjectInfo(bucket, oi), nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// CopyObject copies a blob from source container to destination container.
|
2017-09-05 17:45:22 -04:00
|
|
|
func (l *s3Objects) CopyObject(srcBucket string, srcObject string, dstBucket string, dstObject string, metadata map[string]string) (objInfo ObjectInfo, err error) {
|
|
|
|
// Source object
|
|
|
|
src := minio.NewSourceInfo(srcBucket, srcObject, nil)
|
|
|
|
|
|
|
|
// Destination object
|
|
|
|
var xamzMeta = map[string]string{}
|
|
|
|
for key := range metadata {
|
|
|
|
for _, prefix := range userMetadataKeyPrefixes {
|
|
|
|
if strings.HasPrefix(key, prefix) {
|
|
|
|
xamzMeta[key] = metadata[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst, err := minio.NewDestinationInfo(dstBucket, dstObject, nil, xamzMeta)
|
2017-04-27 14:26:00 -04:00
|
|
|
if err != nil {
|
2017-09-05 17:45:22 -04:00
|
|
|
return objInfo, s3ToObjectError(traceError(err), dstBucket, dstObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = l.Client.CopyObject(dst, src); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return objInfo, s3ToObjectError(traceError(err), srcBucket, srcObject)
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
|
2017-09-05 17:45:22 -04:00
|
|
|
oi, err := l.GetObjectInfo(dstBucket, dstObject)
|
2017-04-27 14:26:00 -04:00
|
|
|
if err != nil {
|
2017-09-05 17:45:22 -04:00
|
|
|
return objInfo, s3ToObjectError(traceError(err), dstBucket, dstObject)
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return oi, nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// DeleteObject deletes a blob in bucket
|
2017-05-15 03:52:33 -04:00
|
|
|
func (l *s3Objects) DeleteObject(bucket string, object string) error {
|
2017-04-27 14:26:00 -04:00
|
|
|
err := l.Client.RemoveObject(bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
return s3ToObjectError(traceError(err), bucket, object)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fromMinioClientUploadMetadata converts ObjectMultipartInfo to uploadMetadata
|
|
|
|
func fromMinioClientUploadMetadata(omi minio.ObjectMultipartInfo) uploadMetadata {
|
|
|
|
return uploadMetadata{
|
|
|
|
Object: omi.Key,
|
|
|
|
UploadID: omi.UploadID,
|
|
|
|
Initiated: omi.Initiated,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fromMinioClientListMultipartsInfo converts minio ListMultipartUploadsResult to ListMultipartsInfo
|
|
|
|
func fromMinioClientListMultipartsInfo(lmur minio.ListMultipartUploadsResult) ListMultipartsInfo {
|
|
|
|
uploads := make([]uploadMetadata, len(lmur.Uploads))
|
|
|
|
|
|
|
|
for i, um := range lmur.Uploads {
|
|
|
|
uploads[i] = fromMinioClientUploadMetadata(um)
|
|
|
|
}
|
|
|
|
|
|
|
|
commonPrefixes := make([]string, len(lmur.CommonPrefixes))
|
|
|
|
for i, cp := range lmur.CommonPrefixes {
|
|
|
|
commonPrefixes[i] = cp.Prefix
|
|
|
|
}
|
|
|
|
|
|
|
|
return ListMultipartsInfo{
|
|
|
|
KeyMarker: lmur.KeyMarker,
|
|
|
|
UploadIDMarker: lmur.UploadIDMarker,
|
|
|
|
NextKeyMarker: lmur.NextKeyMarker,
|
|
|
|
NextUploadIDMarker: lmur.NextUploadIDMarker,
|
|
|
|
MaxUploads: int(lmur.MaxUploads),
|
|
|
|
IsTruncated: lmur.IsTruncated,
|
|
|
|
Uploads: uploads,
|
|
|
|
Prefix: lmur.Prefix,
|
|
|
|
Delimiter: lmur.Delimiter,
|
|
|
|
CommonPrefixes: commonPrefixes,
|
|
|
|
EncodingType: lmur.EncodingType,
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// ListMultipartUploads lists all multipart uploads.
|
2017-06-21 22:53:09 -04:00
|
|
|
func (l *s3Objects) ListMultipartUploads(bucket string, prefix string, keyMarker string, uploadIDMarker string, delimiter string, maxUploads int) (lmi ListMultipartsInfo, e error) {
|
2017-04-27 14:26:00 -04:00
|
|
|
result, err := l.Client.ListMultipartUploads(bucket, prefix, keyMarker, uploadIDMarker, delimiter, maxUploads)
|
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return lmi, err
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return fromMinioClientListMultipartsInfo(result), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fromMinioClientMetadata converts minio metadata to map[string]string
|
|
|
|
func fromMinioClientMetadata(metadata map[string][]string) map[string]string {
|
|
|
|
mm := map[string]string{}
|
|
|
|
for k, v := range metadata {
|
|
|
|
mm[http.CanonicalHeaderKey(k)] = v[0]
|
|
|
|
}
|
|
|
|
return mm
|
|
|
|
}
|
|
|
|
|
|
|
|
// toMinioClientMetadata converts metadata to map[string][]string
|
2017-09-28 11:10:38 -04:00
|
|
|
func toMinioClientMetadata(metadata map[string]string) map[string]string {
|
|
|
|
mm := map[string]string{}
|
2017-04-27 14:26:00 -04:00
|
|
|
for k, v := range metadata {
|
2017-09-28 11:10:38 -04:00
|
|
|
mm[http.CanonicalHeaderKey(k)] = v
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
return mm
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// NewMultipartUpload upload object in multiple parts
|
2017-05-15 03:52:33 -04:00
|
|
|
func (l *s3Objects) NewMultipartUpload(bucket string, object string, metadata map[string]string) (uploadID string, err error) {
|
2017-09-28 11:10:38 -04:00
|
|
|
// Create PutObject options
|
|
|
|
opts := minio.PutObjectOptions{UserMetadata: metadata}
|
|
|
|
return l.Client.NewMultipartUpload(bucket, object, opts)
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// CopyObjectPart copy part of object to other bucket and object
|
2017-05-15 03:52:33 -04:00
|
|
|
func (l *s3Objects) CopyObjectPart(srcBucket string, srcObject string, destBucket string, destObject string, uploadID string, partID int, startOffset int64, length int64) (info PartInfo, err error) {
|
2017-04-27 14:26:00 -04:00
|
|
|
// FIXME: implement CopyObjectPart
|
|
|
|
return PartInfo{}, traceError(NotImplemented{})
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// fromMinioClientObjectPart converts minio ObjectPart to PartInfo
|
2017-04-27 14:26:00 -04:00
|
|
|
func fromMinioClientObjectPart(op minio.ObjectPart) PartInfo {
|
|
|
|
return PartInfo{
|
|
|
|
Size: op.Size,
|
2017-05-08 17:42:05 -04:00
|
|
|
ETag: canonicalizeETag(op.ETag),
|
2017-04-27 14:26:00 -04:00
|
|
|
LastModified: op.LastModified,
|
|
|
|
PartNumber: op.PartNumber,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PutObjectPart puts a part of object in bucket
|
2017-09-19 15:40:27 -04:00
|
|
|
func (l *s3Objects) PutObjectPart(bucket string, object string, uploadID string, partID int, data *HashReader) (pi PartInfo, e error) {
|
|
|
|
md5HexBytes, err := hex.DecodeString(data.md5Sum)
|
|
|
|
if err != nil {
|
|
|
|
return pi, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sha256sumBytes, err := hex.DecodeString(data.sha256Sum)
|
2017-04-27 14:26:00 -04:00
|
|
|
if err != nil {
|
2017-09-19 15:40:27 -04:00
|
|
|
return pi, err
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
|
2017-09-28 11:10:38 -04:00
|
|
|
info, err := l.Client.PutObjectPart(bucket, object, uploadID, partID, data, data.Size(), md5HexBytes, sha256sumBytes)
|
2017-04-27 14:26:00 -04:00
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return pi, err
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return fromMinioClientObjectPart(info), nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// fromMinioClientObjectParts converts minio ObjectPart to PartInfo
|
2017-04-27 14:26:00 -04:00
|
|
|
func fromMinioClientObjectParts(parts []minio.ObjectPart) []PartInfo {
|
|
|
|
toParts := make([]PartInfo, len(parts))
|
|
|
|
for i, part := range parts {
|
|
|
|
toParts[i] = fromMinioClientObjectPart(part)
|
|
|
|
}
|
|
|
|
return toParts
|
|
|
|
}
|
|
|
|
|
|
|
|
// fromMinioClientListPartsInfo converts minio ListObjectPartsResult to ListPartsInfo
|
|
|
|
func fromMinioClientListPartsInfo(lopr minio.ListObjectPartsResult) ListPartsInfo {
|
|
|
|
return ListPartsInfo{
|
|
|
|
UploadID: lopr.UploadID,
|
|
|
|
Bucket: lopr.Bucket,
|
|
|
|
Object: lopr.Key,
|
|
|
|
StorageClass: "",
|
|
|
|
PartNumberMarker: lopr.PartNumberMarker,
|
|
|
|
NextPartNumberMarker: lopr.NextPartNumberMarker,
|
|
|
|
MaxParts: lopr.MaxParts,
|
|
|
|
IsTruncated: lopr.IsTruncated,
|
|
|
|
EncodingType: lopr.EncodingType,
|
|
|
|
Parts: fromMinioClientObjectParts(lopr.ObjectParts),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListObjectParts returns all object parts for specified object in specified bucket
|
2017-06-21 22:53:09 -04:00
|
|
|
func (l *s3Objects) ListObjectParts(bucket string, object string, uploadID string, partNumberMarker int, maxParts int) (lpi ListPartsInfo, e error) {
|
2017-04-27 14:26:00 -04:00
|
|
|
result, err := l.Client.ListObjectParts(bucket, object, uploadID, partNumberMarker, maxParts)
|
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return lpi, err
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return fromMinioClientListPartsInfo(result), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AbortMultipartUpload aborts a ongoing multipart upload
|
2017-05-15 03:52:33 -04:00
|
|
|
func (l *s3Objects) AbortMultipartUpload(bucket string, object string, uploadID string) error {
|
2017-04-27 14:26:00 -04:00
|
|
|
return l.Client.AbortMultipartUpload(bucket, object, uploadID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// toMinioClientCompletePart converts completePart to minio CompletePart
|
|
|
|
func toMinioClientCompletePart(part completePart) minio.CompletePart {
|
|
|
|
return minio.CompletePart{
|
|
|
|
ETag: part.ETag,
|
|
|
|
PartNumber: part.PartNumber,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// toMinioClientCompleteParts converts []completePart to minio []CompletePart
|
2017-04-27 14:26:00 -04:00
|
|
|
func toMinioClientCompleteParts(parts []completePart) []minio.CompletePart {
|
|
|
|
mparts := make([]minio.CompletePart, len(parts))
|
|
|
|
for i, part := range parts {
|
|
|
|
mparts[i] = toMinioClientCompletePart(part)
|
|
|
|
}
|
|
|
|
return mparts
|
|
|
|
}
|
|
|
|
|
|
|
|
// CompleteMultipartUpload completes ongoing multipart upload and finalizes object
|
2017-06-21 22:53:09 -04:00
|
|
|
func (l *s3Objects) CompleteMultipartUpload(bucket string, object string, uploadID string, uploadedParts []completePart) (oi ObjectInfo, e error) {
|
2017-04-27 14:26:00 -04:00
|
|
|
err := l.Client.CompleteMultipartUpload(bucket, object, uploadID, toMinioClientCompleteParts(uploadedParts))
|
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, s3ToObjectError(traceError(err), bucket, object)
|
2017-04-27 14:26:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return l.GetObjectInfo(bucket, object)
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// SetBucketPolicies sets policy on bucket
|
2017-05-15 03:52:33 -04:00
|
|
|
func (l *s3Objects) SetBucketPolicies(bucket string, policyInfo policy.BucketAccessPolicy) error {
|
2017-04-27 14:26:00 -04:00
|
|
|
if err := l.Client.PutBucketPolicy(bucket, policyInfo); err != nil {
|
|
|
|
return s3ToObjectError(traceError(err), bucket, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// GetBucketPolicies will get policy on bucket
|
2017-05-15 03:52:33 -04:00
|
|
|
func (l *s3Objects) GetBucketPolicies(bucket string) (policy.BucketAccessPolicy, error) {
|
2017-04-27 14:26:00 -04:00
|
|
|
policyInfo, err := l.Client.GetBucketPolicy(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return policy.BucketAccessPolicy{}, s3ToObjectError(traceError(err), bucket, "")
|
|
|
|
}
|
|
|
|
return policyInfo, nil
|
|
|
|
}
|
|
|
|
|
2017-05-01 17:44:31 -04:00
|
|
|
// DeleteBucketPolicies deletes all policies on bucket
|
2017-05-15 03:52:33 -04:00
|
|
|
func (l *s3Objects) DeleteBucketPolicies(bucket string) error {
|
2017-04-27 14:26:00 -04:00
|
|
|
if err := l.Client.PutBucketPolicy(bucket, policy.BucketAccessPolicy{}); err != nil {
|
|
|
|
return s3ToObjectError(traceError(err), bucket, "")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|