From 1bb4aa62ea09633930e3f34c736afc0111a6ef52 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 10 Jun 2015 00:37:09 -0700 Subject: [PATCH] update to new error related changes in minio-go --- Godeps/Godeps.json | 2 +- .../src/github.com/minio/minio-go/README.md | 10 +- .../src/github.com/minio/minio-go/api.go | 91 +++++++++---------- .../src/github.com/minio/minio-go/api_test.go | 2 +- .../src/github.com/minio/minio-go/errors.go | 33 +++++++ .../github.com/minio/minio-go/lowlevelapi.go | 41 ++++++++- pkg/client/s3/s3.go | 2 +- 7 files changed, 123 insertions(+), 58 deletions(-) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 7125f23c..f923fd2c 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -28,7 +28,7 @@ }, { "ImportPath": "github.com/minio/minio-go", - "Rev": "dee5d16f0c4515ddb2785a1b8317ec11796d8fc3" + "Rev": "2313097b2debae7b284b4bedb44e2fafa6a26e12" }, { "ImportPath": "github.com/minio/minio/pkg/iodine", diff --git a/Godeps/_workspace/src/github.com/minio/minio-go/README.md b/Godeps/_workspace/src/github.com/minio/minio-go/README.md index 2ebb3b7e..8ad02713 100644 --- a/Godeps/_workspace/src/github.com/minio/minio-go/README.md +++ b/Godeps/_workspace/src/github.com/minio/minio-go/README.md @@ -37,8 +37,16 @@ func main() { ## Documentation +* [PutObject(string, string, int64, io.Reader) error](examples/s3/putobject.go) +* [GetObject(string, string, int64, int64](examples/s3/getobject.go) +* [ListObjects(string, string, bool) <-chan](examples/s3/listobjects.go) +* [ListBuckets() <-chan](examples/s3/listbuckets.go) + +### API Reference + +[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/minio/minio-go) + * API reference [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/minio/minio-go) -* Complete example. [putobject.go](https://github.com/minio/minio-go/blob/master/examples/s3/putobject.go) ## Join The Community * Community hangout on Gitter [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/minio/minio?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) diff --git a/Godeps/_workspace/src/github.com/minio/minio-go/api.go b/Godeps/_workspace/src/github.com/minio/minio-go/api.go index 1172d900..eda2899a 100644 --- a/Godeps/_workspace/src/github.com/minio/minio-go/api.go +++ b/Godeps/_workspace/src/github.com/minio/minio-go/api.go @@ -19,7 +19,6 @@ package minio import ( "encoding/hex" "errors" - "fmt" "io" "net/http" "net/url" @@ -27,7 +26,6 @@ import ( "sort" "strings" "time" - "unicode/utf8" ) // API - object storage API interface @@ -41,7 +39,7 @@ type API interface { // BucketAPI - bucket specific Read/Write/Stat interface type BucketAPI interface { - MakeBucket(bucket string, cannedACL BucketACL, location string) error + MakeBucket(bucket string, cannedACL BucketACL) error BucketExists(bucket string) error RemoveBucket(bucket string) error SetBucketACL(bucket string, cannedACL BucketACL) error @@ -128,8 +126,8 @@ func getRegion(endPoint string) (region string, err error) { return regions[u.Host], nil } - // Region cannot be empty according to Amazon S3 standard. So - // we address all the four quadrants of our galaxy. + // Region cannot be empty according to Amazon S3 standard. + // So we address all the four quadrants of our galaxy. return "milkyway", nil } @@ -144,22 +142,23 @@ type Config struct { // Advanced options AcceptType string // specify this to get server response in non XML style if server supports it Transport http.RoundTripper // custom transport usually for debugging, by default its nil + // internal - // use AddUserAgent append to default, useful when minio-go is used with in your application + // use SetUserAgent append to default, useful when minio-go is used with in your application userAgent string } // Global constants const ( LibraryName = "minio-go" - LibraryVersion = "0.1" + LibraryVersion = "0.1.0" ) // SetUserAgent - append to a default user agent func (c *Config) SetUserAgent(name string, version string, comments ...string) { // if no name and version is set we do not add new user agents if name != "" && version != "" { - c.userAgent = c.userAgent + " " + name + "/" + version + " (" + strings.Join(comments, ", ") + ") " + c.userAgent = c.userAgent + " " + name + "/" + version + " (" + strings.Join(comments, "; ") + ") " } } @@ -187,20 +186,8 @@ func New(config Config) (API, error) { // Additionally it also takes range arguments to download the specified range bytes of an object. // For more information about the HTTP Range header, go to http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35. func (a api) GetObject(bucket, object string, offset, length int64) (io.ReadCloser, ObjectStat, error) { - if strings.TrimSpace(object) == "" { - return nil, ObjectStat{}, errors.New("object name cannot be empty") - } - if !utf8.ValidString(object) { - return nil, ObjectStat{}, errors.New("invalid object name, should be utf-8") - } // get the the object - // NOTE : returned md5sum could be the md5sum of the partial object itself - // not the whole object depending on if offset range was requested or not - body, objectMetadata, err := a.getObject(bucket, object, offset, length) - if err != nil { - return nil, ObjectStat{}, err - } - return body, objectMetadata, nil + return a.getObject(bucket, object, offset, length) } // completedParts is a wrapper to make parts sortable by their part number @@ -418,11 +405,8 @@ func (a api) listMultipartUploadsRecursiveInRoutine(bucket, prefix string, ch ch // // This version of PutObject automatically does multipart for more than 5MB worth of data func (a api) PutObject(bucket, object string, size int64, data io.Reader) error { - if strings.TrimSpace(object) == "" { - return errors.New("object name cannot be empty") - } - if !utf8.ValidString(object) { - return errors.New("invalid object name, should be utf-8") + if err := invalidArgumentToError(object); err != nil { + return err } switch { case size < MinimumPartSize: @@ -460,23 +444,11 @@ func (a api) PutObject(bucket, object string, size int64, data io.Reader) error // StatObject verify if object exists and you have permission to access it func (a api) StatObject(bucket, object string) (ObjectStat, error) { - if strings.TrimSpace(object) == "" { - return ObjectStat{}, errors.New("object name cannot be empty") - } - if !utf8.ValidString(object) { - return ObjectStat{}, errors.New("invalid object name, should be utf-8") - } return a.headObject(bucket, object) } // RemoveObject remove the object from a bucket func (a api) RemoveObject(bucket, object string) error { - if strings.TrimSpace(object) == "" { - return errors.New("object name cannot be empty") - } - if !utf8.ValidString(object) { - return errors.New("invalid object name, should be utf-8") - } return a.deleteObject(bucket, object) } @@ -495,18 +467,17 @@ func (a api) RemoveObject(bucket, object string) error { // public-read-write - owner gets full access, all others get full access too // authenticated-read - owner gets full access, authenticated users get read access // -// Location valid values +// Location valid values which are automatically derived from config endpoint // // [ us-west-1 | us-west-2 | eu-west-1 | eu-central-1 | ap-southeast-1 | ap-northeast-1 | ap-southeast-2 | sa-east-1 ] // Default - US standard -func (a api) MakeBucket(bucket string, acl BucketACL, location string) error { +func (a api) MakeBucket(bucket string, acl BucketACL) error { if !acl.isValidBucketACL() { - return fmt.Errorf("%s", "Invalid bucket ACL") + return invalidArgumentToError("") } - if _, ok := regions[location]; !ok { - if location != "" { - return fmt.Errorf("%s", "Invalid bucket Location") - } + location, _ := getRegion(a.config.Endpoint) + if location == "milkyway" { + location = "" } return a.putBucket(bucket, string(acl), location) } @@ -522,7 +493,7 @@ func (a api) MakeBucket(bucket string, acl BucketACL, location string) error { // func (a api) SetBucketACL(bucket string, acl BucketACL) error { if !acl.isValidBucketACL() { - return fmt.Errorf("%s", "Invalid bucket ACL") + return invalidArgumentToError("") } return a.putBucketACL(bucket, string(acl)) } @@ -537,13 +508,13 @@ func (a api) SetBucketACL(bucket string, acl BucketACL) error { // authenticated-read - owner gets full access, authenticated users get read access // func (a api) GetBucketACL(bucket string) (BucketACL, error) { + if err := invalidBucketToError(bucket); err != nil { + return "", err + } policy, err := a.getBucketACL(bucket) if err != nil { return "", err } - if policy.AccessControlList.Grant == nil { - return "", fmt.Errorf("%s", "Unexpected error") - } grants := policy.AccessControlList.Grant switch { case len(grants) == 1: @@ -566,7 +537,12 @@ func (a api) GetBucketACL(bucket string) (BucketACL, error) { } } } - return "", fmt.Errorf("Cannot verify access control policy") + return "", ErrorResponse{ + Code: "NoSuchBucketPolicy", + Message: "The specified bucket does not have a bucket policy.", + Resource: "/" + bucket, + RequestID: "minio", + } } // BucketExists verify if bucket exists and you have permission to access it @@ -585,6 +561,13 @@ func (a api) RemoveBucket(bucket string) error { // listObjectsInRoutine is an internal goroutine function called for listing objects // This function feeds data into channel func (a api) listObjectsInRoutine(bucket, prefix string, recursive bool, ch chan ObjectStatCh) { + if err := invalidBucketToError(bucket); err != nil { + ch <- ObjectStatCh{ + Stat: ObjectStat{}, + Err: err, + } + return + } defer close(ch) switch { case recursive == true: @@ -698,6 +681,10 @@ func (a api) ListBuckets() <-chan BucketStatCh { func (a api) dropIncompleteUploadsInRoutine(bucket, prefix string, errorCh chan error) { defer close(errorCh) + if err := invalidBucketToError(bucket); err != nil { + errorCh <- err + return + } listMultipartUploadsResult, err := a.listMultipartUploads(bucket, "", "", prefix, "", 1000) if err != nil { errorCh <- err @@ -748,6 +735,10 @@ func (a api) DropIncompleteUploads(bucket, prefix string) <-chan error { func (a api) dropAllIncompleteUploadsInRoutine(bucket string, errorCh chan error) { defer close(errorCh) + if err := invalidBucketToError(bucket); err != nil { + errorCh <- err + return + } listMultipartUploadsResult, err := a.listMultipartUploads(bucket, "", "", "", "", 1000) if err != nil { errorCh <- err diff --git a/Godeps/_workspace/src/github.com/minio/minio-go/api_test.go b/Godeps/_workspace/src/github.com/minio/minio-go/api_test.go index 83187253..905678c9 100644 --- a/Godeps/_workspace/src/github.com/minio/minio-go/api_test.go +++ b/Godeps/_workspace/src/github.com/minio/minio-go/api_test.go @@ -75,7 +75,7 @@ func TestBucketOperations(t *testing.T) { if err != nil { t.Errorf("Error") } - err = a.MakeBucket("bucket", "private", "") + err = a.MakeBucket("bucket", "private") if err != nil { t.Errorf("Error") } diff --git a/Godeps/_workspace/src/github.com/minio/minio-go/errors.go b/Godeps/_workspace/src/github.com/minio/minio-go/errors.go index 83506fb1..5725ff18 100644 --- a/Godeps/_workspace/src/github.com/minio/minio-go/errors.go +++ b/Godeps/_workspace/src/github.com/minio/minio-go/errors.go @@ -19,6 +19,8 @@ package minio import ( "encoding/xml" "io" + "strings" + "unicode/utf8" ) /* **** SAMPLE ERROR RESPONSE **** @@ -89,3 +91,34 @@ func responseToError(body io.Reader) error { } return respError } + +func invalidBucketToError(bucket string) error { + if strings.TrimSpace(bucket) == "" || bucket == "" { + // no resource since bucket is empty string + errorResponse := ErrorResponse{ + Code: "InvalidBucketName", + Message: "The specified bucket is not valid.", + RequestID: "minio", + } + return errorResponse + } + return nil +} + +func invalidArgumentToError(arg string) error { + errorResponse := ErrorResponse{ + Code: "InvalidArgument", + Message: "Invalid Argument", + RequestID: "minio", + } + if strings.TrimSpace(arg) == "" || arg == "" { + // no resource since arg is empty string + return errorResponse + } + if !utf8.ValidString(arg) { + // add resource to reply back with invalid string + errorResponse.Resource = arg + return errorResponse + } + return nil +} diff --git a/Godeps/_workspace/src/github.com/minio/minio-go/lowlevelapi.go b/Godeps/_workspace/src/github.com/minio/minio-go/lowlevelapi.go index 97533124..363d7eb1 100644 --- a/Godeps/_workspace/src/github.com/minio/minio-go/lowlevelapi.go +++ b/Godeps/_workspace/src/github.com/minio/minio-go/lowlevelapi.go @@ -119,7 +119,7 @@ func (a lowLevelAPI) putBucket(bucket, acl, location string) error { } // putBucketRequestACL wrapper creates a new putBucketACL request -func (a lowLevelAPI) putBucketRequestACL(bucket, acl string) (*request, error) { +func (a lowLevelAPI) putBucketACLRequest(bucket, acl string) (*request, error) { op := &operation{ HTTPServer: a.config.Endpoint, HTTPMethod: "PUT", @@ -135,7 +135,7 @@ func (a lowLevelAPI) putBucketRequestACL(bucket, acl string) (*request, error) { // putBucketACL set the permissions on an existing bucket using access control lists (ACL) func (a lowLevelAPI) putBucketACL(bucket, acl string) error { - req, err := a.putBucketRequestACL(bucket, acl) + req, err := a.putBucketACLRequest(bucket, acl) if err != nil { return err } @@ -153,7 +153,7 @@ func (a lowLevelAPI) putBucketACL(bucket, acl string) error { } // getBucketACLRequest wrapper creates a new getBucketACL request -func (a lowLevelAPI) getBucketRequestACL(bucket string) (*request, error) { +func (a lowLevelAPI) getBucketACLRequest(bucket string) (*request, error) { op := &operation{ HTTPServer: a.config.Endpoint, HTTPMethod: "GET", @@ -168,7 +168,7 @@ func (a lowLevelAPI) getBucketRequestACL(bucket string) (*request, error) { // getBucketACL get the acl information on an existing bucket func (a lowLevelAPI) getBucketACL(bucket string) (accessControlPolicy, error) { - req, err := a.getBucketRequestACL(bucket) + req, err := a.getBucketACLRequest(bucket) if err != nil { return accessControlPolicy{}, err } @@ -188,6 +188,15 @@ func (a lowLevelAPI) getBucketACL(bucket string) (accessControlPolicy, error) { if err != nil { return accessControlPolicy{}, err } + if policy.AccessControlList.Grant == nil { + errorResponse := ErrorResponse{ + Code: "InternalError", + Message: "Unexpected error please report this at https://github.com/minio/minio-go/issues", + Resource: "/" + bucket, + RequestID: "minio", + } + return accessControlPolicy{}, errorResponse + } return policy, nil } @@ -288,6 +297,9 @@ func (a lowLevelAPI) listObjectsRequest(bucket, marker, prefix, delimiter string // ?prefix - Limits the response to keys that begin with the specified prefix. // ?max-keys - Sets the maximum number of keys returned in the response body. func (a lowLevelAPI) listObjects(bucket, marker, prefix, delimiter string, maxkeys int) (listBucketResult, error) { + if err := invalidBucketToError(bucket); err != nil { + return listBucketResult{}, err + } req, err := a.listObjectsRequest(bucket, marker, prefix, delimiter, maxkeys) if err != nil { return listBucketResult{}, err @@ -323,6 +335,9 @@ func (a lowLevelAPI) headBucketRequest(bucket string) (*request, error) { // headBucket - useful to determine if a bucket exists and you have permission to access it. func (a lowLevelAPI) headBucket(bucket string) error { + if err := invalidBucketToError(bucket); err != nil { + return err + } req, err := a.headBucketRequest(bucket) if err != nil { return err @@ -373,6 +388,9 @@ func (a lowLevelAPI) deleteBucketRequest(bucket string) (*request, error) { // All objects (including all object versions and delete markers) // in the bucket must be deleted before successfully attempting this request func (a lowLevelAPI) deleteBucket(bucket string) error { + if err := invalidBucketToError(bucket); err != nil { + return err + } req, err := a.deleteBucketRequest(bucket) if err != nil { return err @@ -484,6 +502,9 @@ func (a lowLevelAPI) getObjectRequest(bucket, object string, offset, length int6 // Additionally it also takes range arguments to download the specified range bytes of an object. // For more information about the HTTP Range header, go to http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35. func (a lowLevelAPI) getObject(bucket, object string, offset, length int64) (io.ReadCloser, ObjectStat, error) { + if err := invalidArgumentToError(object); err != nil { + return nil, ObjectStat{}, err + } req, err := a.getObjectRequest(bucket, object, offset, length) if err != nil { return nil, ObjectStat{}, err @@ -534,6 +555,12 @@ func (a lowLevelAPI) deleteObjectRequest(bucket, object string) (*request, error // deleteObject deletes a given object from a bucket func (a lowLevelAPI) deleteObject(bucket, object string) error { + if err := invalidBucketToError(bucket); err != nil { + return err + } + if err := invalidArgumentToError(object); err != nil { + return err + } req, err := a.deleteObjectRequest(bucket, object) if err != nil { return err @@ -584,6 +611,12 @@ func (a lowLevelAPI) headObjectRequest(bucket, object string) (*request, error) // headObject - retrieves metadata from an object without returning the object itself func (a lowLevelAPI) headObject(bucket, object string) (ObjectStat, error) { + if err := invalidBucketToError(bucket); err != nil { + return ObjectStat{}, err + } + if err := invalidArgumentToError(object); err != nil { + return ObjectStat{}, err + } req, err := a.headObjectRequest(bucket, object) if err != nil { return ObjectStat{}, err diff --git a/pkg/client/s3/s3.go b/pkg/client/s3/s3.go index 317cd8ba..38f0ad77 100644 --- a/pkg/client/s3/s3.go +++ b/pkg/client/s3/s3.go @@ -146,7 +146,7 @@ func (c *s3Client) MakeBucket() error { return iodine.New(InvalidQueryURL{URL: c.hostURL.String()}, nil) } // location string is intentionally left out - err := c.api.MakeBucket(bucket, s3.BucketACL("private"), "") + err := c.api.MakeBucket(bucket, s3.BucketACL("private")) return iodine.New(err, nil) }