From 0345c8fffb2741f9c7bcbe7d67ac302ed464d18d Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sun, 27 Dec 2015 00:38:38 -0700 Subject: [PATCH] bucket-location: Implement bucket location response. --- api-definitions.go | 22 ++++++++++++++-------- bucket-handlers.go | 35 +++++++++++++++++++++++++++++++++++ routers.go | 1 + 3 files changed, 50 insertions(+), 8 deletions(-) diff --git a/api-definitions.go b/api-definitions.go index b820490a5..f5ccc0de5 100644 --- a/api-definitions.go +++ b/api-definitions.go @@ -18,12 +18,18 @@ package main import "encoding/xml" -// Limit number of objects in a given response +// Limit number of objects in a given response. const ( maxObjectList = 1000 ) -// AccessControlPolicyResponse - format for get bucket acl response +// LocationResponse - format for location response. +type LocationResponse struct { + XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ LocationConstraint" json:"-"` + Location string `xml:",chardata"` +} + +// AccessControlPolicyResponse - format for get bucket acl response. type AccessControlPolicyResponse struct { XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ AccessControlPolicy" json:"-"` @@ -33,7 +39,7 @@ type AccessControlPolicyResponse struct { Owner Owner } -// Grant container for grantee and permission +// Grant container for grantee and permission. type Grant struct { Grantee struct { ID string @@ -45,7 +51,7 @@ type Grant struct { Permission string } -// ListObjectsResponse - format for list objects response +// ListObjectsResponse - format for list objects response. type ListObjectsResponse struct { XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListBucketResult" json:"-"` @@ -75,7 +81,7 @@ type ListObjectsResponse struct { Prefix string } -// Part container for part metadata +// Part container for part metadata. type Part struct { PartNumber int ETag string @@ -83,7 +89,7 @@ type Part struct { Size int64 } -// ListPartsResponse - format for list parts response +// ListPartsResponse - format for list parts response. type ListPartsResponse struct { XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListPartsResult" json:"-"` @@ -102,11 +108,11 @@ type ListPartsResponse struct { MaxParts int IsTruncated bool - // List of parts + // List of parts. Part []*Part } -// ListMultipartUploadsResponse - format for list multipart uploads response +// ListMultipartUploadsResponse - format for list multipart uploads response. type ListMultipartUploadsResponse struct { XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ ListMultipartUploadsResult" json:"-"` diff --git a/bucket-handlers.go b/bucket-handlers.go index 8a9eb003a..9a4cf64c9 100644 --- a/bucket-handlers.go +++ b/bucket-handlers.go @@ -27,6 +27,41 @@ import ( "github.com/minio/minio/pkg/fs" ) +// GetBucketLocationHandler - GET Bucket location. +// ------------------------- +// This operation returns bucket location. +func (api CloudStorageAPI) GetBucketLocationHandler(w http.ResponseWriter, req *http.Request) { + vars := mux.Vars(req) + bucket := vars["bucket"] + + if !api.Anonymous { + if isRequestRequiresACLCheck(req) { + writeErrorResponse(w, req, AccessDenied, req.URL.Path) + return + } + } + + _, err := api.Filesystem.GetBucketMetadata(bucket) + if err != nil { + errorIf(err.Trace(), "GetBucketMetadata failed.", nil) + switch err.ToGoError().(type) { + case fs.BucketNotFound: + writeErrorResponse(w, req, NoSuchBucket, req.URL.Path) + case fs.BucketNameInvalid: + writeErrorResponse(w, req, InvalidBucketName, req.URL.Path) + default: + writeErrorResponse(w, req, InternalError, req.URL.Path) + } + } + + // TODO: Location value for LocationResponse is deliberately not used, until + // we bring in a mechanism of configurable regions. For the time being + // default region is empty i.e 'us-east-1'. + encodedSuccessResponse := encodeSuccessResponse(LocationResponse{}) // generate response + setCommonHeaders(w, len(encodedSuccessResponse)) // write headers + w.Write(encodedSuccessResponse) // write body +} + // ListMultipartUploadsHandler - GET Bucket (List Multipart uploads) // ------------------------- // This operation lists in-progress multipart uploads. An in-progress diff --git a/routers.go b/routers.go index 758bbf849..a18c8fa14 100644 --- a/routers.go +++ b/routers.go @@ -49,6 +49,7 @@ func registerCloudStorageAPI(mux *router.Router, a CloudStorageAPI) { bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(a.DeleteObjectHandler) // Bucket operations + bucket.Methods("GET").HandlerFunc(a.GetBucketLocationHandler).Queries("location", "") bucket.Methods("GET").HandlerFunc(a.GetBucketACLHandler).Queries("acl", "") bucket.Methods("GET").HandlerFunc(a.ListMultipartUploadsHandler).Queries("uploads", "") bucket.Methods("GET").HandlerFunc(a.ListObjectsHandler)