api: Simplify copyObject API, return only error if any.

- Add a new example and update API spec.
- Fix signV2 test being used inside V4 in previous patch.
This commit is contained in:
Harshavardhana 2016-03-23 18:56:09 -07:00
parent 3680f1e285
commit a12165af80
6 changed files with 247 additions and 36 deletions

37
API.md
View File

@ -35,6 +35,7 @@ s3Client can be used to perform operations on S3 storage. APIs are described bel
* [`GetObject`](#GetObject)
* [`PutObject`](#PutObject)
* [`CopyObject`](#CopyObject)
* [`StatObject`](#StatObject)
* [`RemoveObject`](#RemoveObject)
* [`RemoveIncompleteUpload`](#RemoveIncompleteUpload)
@ -328,6 +329,42 @@ if err != nil {
}
```
---------------------------------------
<a name="CopyObject">
#### CopyObject(bucketName, objectName, objectSource, conditions)
Copy a source object into a new object with the provided name in the provided bucket.
__Arguments__
* `bucketName` _string_: name of the bucket
* `objectName` _string_: name of the object
* `objectSource` _string_: name of the object source.
* `conditions` _CopyConditions_: Collection of supported CopyObject conditions. ['x-amz-copy-source', 'x-amz-copy-source-if-match', 'x-amz-copy-source-if-none-match', 'x-amz-copy-source-if-unmodified-since', 'x-amz-copy-source-if-modified-since']
__Example__
```go
// All following conditions are allowed and can be combined together.
// Set copy conditions.
var copyConds = minio.NewCopyConditions()
// Set modified condition, copy object modified since 2014 April.
copyConds.SetModified(time.Date(2014, time.April, 0, 0, 0, 0, 0, time.UTC))
// Set unmodified condition, copy object unmodified since 2014 April.
// copyConds.SetUnmodified(time.Date(2014, time.April, 0, 0, 0, 0, 0, time.UTC))
// Set matching ETag condition, copy object which matches the following ETag.
// copyConds.SetMatchETag("31624deb84149d2f8ef9c385918b653a")
// Set matching ETag except condition, copy object which does not match the following ETag.
// copyConds.SetMatchETagExcept("31624deb84149d2f8ef9c385918b653a")
err := s3Client.CopyObject("my-bucketname", "my-objectname", "/my-sourcebucketname/my-sourceobjectname", copyConds)
if err != nil {
fmt.Println(err)
return
}
```
---------------------------------------
<a name="FPutObject">
#### FPutObject(bucketName, objectName, filePath, contentType)

View File

@ -1,5 +1,5 @@
/*
* Minio Go Library for Amazon S3 Compatible Cloud Storage (C) 2015, 2016 Minio, Inc.
* Minio Go Library for Amazon S3 Compatible Cloud Storage (C) 2016 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -19,36 +19,50 @@ package minio
import "net/http"
// CopyObject - copy a source object into a new object with the provided name in the provided bucket
func (c Client) CopyObject(bucketName, objectName string, xAmzHeaders http.Header) (copyObjectResult, error) {
func (c Client) CopyObject(bucketName string, objectName string, objectSource string, cpCond CopyConditions) error {
// Input validation.
if err := isValidBucketName(bucketName); err != nil {
return copyObjectResult{}, err
return err
}
if err := isValidObjectName(objectName); err != nil {
return copyObjectResult{}, err
return err
}
if objectSource == "" {
return ErrInvalidArgument("Object source cannot be empty.")
}
// customHeaders apply headers.
customHeaders := make(http.Header)
for _, cond := range cpCond.conditions {
customHeaders.Set(cond.key, cond.value)
}
// Set copy source.
customHeaders.Set("x-amz-copy-source", objectSource)
// Execute PUT on objectName.
resp, err := c.executeMethod("PUT", requestMetadata{
bucketName: bucketName,
objectName: objectName,
customHeader: xAmzHeaders,
customHeader: customHeaders,
})
defer closeResponse(resp)
if err != nil {
return copyObjectResult{}, err
return err
}
if resp != nil {
if resp.StatusCode != http.StatusOK {
return copyObjectResult{}, httpRespToErrorResponse(resp, bucketName, objectName)
return httpRespToErrorResponse(resp, bucketName, objectName)
}
}
// Decode copy response on success.
copyObjectResult := copyObjectResult{}
err = xmlDecoder(resp.Body, &copyObjectResult)
cpObjRes := copyObjectResult{}
err = xmlDecoder(resp.Body, &cpObjRes)
if err != nil {
return copyObjectResult, err
return err
}
return copyObjectResult, nil
// Return nil on success.
return nil
}

View File

@ -26,7 +26,6 @@ import (
"net/http"
"net/url"
"os"
"strings"
"testing"
"time"
@ -958,12 +957,18 @@ func TestCopyObjectV2(t *testing.T) {
len(buf), n)
}
// Make headers for the copy.
xAmzHeaders := make(http.Header)
xAmzHeaders.Set("x-amz-copy-source", bucketName+"/"+objectName)
// Set copy conditions.
copyConds := minio.NewCopyConditions()
err = copyConds.SetModified(time.Date(2014, time.April, 0, 0, 0, 0, 0, time.UTC))
if err != nil {
t.Fatal("Error:", err)
}
// Copy source.
copySource := bucketName + "/" + objectName
// Perform the Copy
res, err := c.CopyObject(bucketName+"-copy", objectName+"-copy", xAmzHeaders)
err = c.CopyObject(bucketName+"-copy", objectName+"-copy", copySource, copyConds)
if err != nil {
t.Fatal("Error:", err, bucketName+"-copy", objectName+"-copy")
}
@ -995,13 +1000,6 @@ func TestCopyObjectV2(t *testing.T) {
t.Fatalf("Error: ETags do not match, want %v, got %v\n",
objInfoCopy.ETag, objInfo.ETag)
}
// Trim double quotes from copyObjectResult ETag field to compare
copyObjectETag := strings.TrimPrefix(res.ETag, "\"")
copyObjectETag = strings.TrimSuffix(copyObjectETag, "\"")
if copyObjectETag != objInfo.ETag {
t.Fatalf("Error: ETags do not match, want %v, got %v\n",
objInfo.ETag, copyObjectETag)
}
// Remove all objects and buckets
err = c.RemoveObject(bucketName, objectName)

View File

@ -26,7 +26,6 @@ import (
"net/http"
"net/url"
"os"
"strings"
"testing"
"time"
@ -936,7 +935,7 @@ func TestCopyObject(t *testing.T) {
rand.Seed(time.Now().Unix())
// Instantiate new minio client object
c, err := minio.NewV2(
c, err := minio.NewV4(
"s3.amazonaws.com",
os.Getenv("ACCESS_KEY"),
os.Getenv("SECRET_KEY"),
@ -987,12 +986,18 @@ func TestCopyObject(t *testing.T) {
len(buf), n)
}
// Make headers for the copy.
xAmzHeaders := make(http.Header)
xAmzHeaders.Set("x-amz-copy-source", bucketName+"/"+objectName)
// Set copy conditions.
copyConds := minio.NewCopyConditions()
err = copyConds.SetModified(time.Date(2014, time.April, 0, 0, 0, 0, 0, time.UTC))
if err != nil {
t.Fatal("Error:", err)
}
// Copy source.
copySource := bucketName + "/" + objectName
// Perform the Copy
res, err := c.CopyObject(bucketName+"-copy", objectName+"-copy", xAmzHeaders)
err = c.CopyObject(bucketName+"-copy", objectName+"-copy", copySource, copyConds)
if err != nil {
t.Fatal("Error:", err, bucketName+"-copy", objectName+"-copy")
}
@ -1024,13 +1029,6 @@ func TestCopyObject(t *testing.T) {
t.Fatalf("Error: ETags do not match, want %v, got %v\n",
objInfoCopy.ETag, objInfo.ETag)
}
// Trim double quotes from copyObjectResult ETag field to compare
copyObjectETag := strings.TrimPrefix(res.ETag, "\"")
copyObjectETag = strings.TrimSuffix(copyObjectETag, "\"")
if copyObjectETag != objInfo.ETag {
t.Fatalf("Error: ETags do not match, want %v, got %v\n",
objInfo.ETag, copyObjectETag)
}
// Remove all objects and buckets
err = c.RemoveObject(bucketName, objectName)

97
copy-conditions.go Normal file
View File

@ -0,0 +1,97 @@
/*
* Minio Go Library for Amazon S3 Compatible Cloud Storage (C) 2016 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 minio
import (
"net/http"
"time"
)
// copyCondition explanation:
// http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html
//
// Example:
//
// copyCondition {
// key: "x-amz-copy-if-modified-since",
// value: "Tue, 15 Nov 1994 12:45:26 GMT",
// }
//
type copyCondition struct {
key string
value string
}
// CopyConditions - copy conditions.
type CopyConditions struct {
conditions []copyCondition
}
// NewCopyConditions - Instantiate new list of conditions.
func NewCopyConditions() CopyConditions {
return CopyConditions{
conditions: make([]copyCondition, 0),
}
}
// SetMatchETag - set match etag.
func (c CopyConditions) SetMatchETag(etag string) error {
if etag == "" {
return ErrInvalidArgument("ETag cannot be empty.")
}
c.conditions = append(c.conditions, copyCondition{
key: "x-amz-copy-source-if-match",
value: etag,
})
return nil
}
// SetMatchETagExcept - set match etag except.
func (c CopyConditions) SetMatchETagExcept(etag string) error {
if etag == "" {
return ErrInvalidArgument("ETag cannot be empty.")
}
c.conditions = append(c.conditions, copyCondition{
key: "x-amz-copy-source-if-none-match",
value: etag,
})
return nil
}
// SetUnmodified - set unmodified time since.
func (c CopyConditions) SetUnmodified(modTime time.Time) error {
if modTime.IsZero() {
return ErrInvalidArgument("Modified since cannot be empty.")
}
c.conditions = append(c.conditions, copyCondition{
key: "x-amz-copy-source-if-unmodified-since",
value: modTime.Format(http.TimeFormat),
})
return nil
}
// SetModified - set modified time since.
func (c CopyConditions) SetModified(modTime time.Time) error {
if modTime.IsZero() {
return ErrInvalidArgument("Modified since cannot be empty.")
}
c.conditions = append(c.conditions, copyCondition{
key: "x-amz-copy-source-if-modified-since",
value: modTime.Format(http.TimeFormat),
})
return nil
}

67
examples/s3/copyobject.go Normal file
View File

@ -0,0 +1,67 @@
// +build ignore
/*
* Minio Go Library for Amazon S3 Compatible Cloud Storage (C) 2016 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 main
import (
"log"
"time"
"github.com/minio/minio-go"
)
func main() {
// Note: YOUR-ACCESSKEYID, YOUR-SECRETACCESSKEY, my-testfile, my-bucketname and
// my-objectname are dummy values, please replace them with original values.
// Requests are always secure (HTTPS) by default. Set insecure=true to enable insecure (HTTP) access.
// This boolean value is the last argument for New().
// New returns an Amazon S3 compatible client object. API copatibality (v2 or v4) is automatically
// determined based on the Endpoint value.
s3Client, err := minio.New("s3.amazonaws.com", "YOUR-ACCESSKEYID", "YOUR-SECRETACCESSKEY", false)
if err != nil {
log.Fatalln(err)
}
// Enable trace.
// s3Client.TraceOn(os.Stderr)
// All following conditions are allowed and can be combined together.
// Set copy conditions.
var copyConds = minio.NewCopyConditions()
// Set modified condition, copy object modified since 2014 April.
copyConds.SetModified(time.Date(2014, time.April, 0, 0, 0, 0, 0, time.UTC))
// Set unmodified condition, copy object unmodified since 2014 April.
// copyConds.SetUnmodified(time.Date(2014, time.April, 0, 0, 0, 0, 0, time.UTC))
// Set matching ETag condition, copy object which matches the following ETag.
// copyConds.SetMatchETag("31624deb84149d2f8ef9c385918b653a")
// Set matching ETag except condition, copy object which does not match the following ETag.
// copyConds.SetMatchETagExcept("31624deb84149d2f8ef9c385918b653a")
// Initiate copy object.
err = s3Client.CopyObject("my-bucketname", "my-objectname", "/my-sourcebucketname/my-sourceobjectname", copyConds)
if err != nil {
log.Fatalln(err)
}
log.Println("Copied source object /my-sourcebucketname/my-sourceobjectname to destination /my-bucketname/my-objectname Successfully.")
}