From 357a81e879b52720b0c27cc03b0461d920715cdb Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 24 Jan 2015 15:35:01 -0800 Subject: [PATCH] Add bucket to be part of object struct, reply back with BucketNotFound for erroneous Object Put requests. Other minor cleanups, to follow - https://github.com/golang/go/wiki/CodeReviewComments --- pkg/storage/inmemory/inmemory.go | 27 ++++++----- pkg/storage/storage.go | 1 + pkg/storage/storage_errors.go | 20 +++++---- pkg/webapi/minioapi/minioapi.go | 77 +++++++++++++++----------------- 4 files changed, 65 insertions(+), 60 deletions(-) diff --git a/pkg/storage/inmemory/inmemory.go b/pkg/storage/inmemory/inmemory.go index 386492923..645ea1bd3 100644 --- a/pkg/storage/inmemory/inmemory.go +++ b/pkg/storage/inmemory/inmemory.go @@ -5,7 +5,6 @@ import ( "crypto/sha256" "fmt" "io" - "log" "strings" "time" @@ -43,15 +42,21 @@ func (storage *storage) CopyObjectToWriter(w io.Writer, bucket string, object st func (storage *storage) StoreObject(bucket string, key string, data io.Reader) error { objectKey := bucket + ":" + key + + if _, ok := storage.bucketdata[bucket]; ok == false { + return mstorage.BucketNotFound{Bucket: bucket} + } + if _, ok := storage.objectdata[objectKey]; ok == true { return mstorage.ObjectExists{Bucket: bucket, Key: key} } var bytesBuffer bytes.Buffer - newObject := storedObject{} + var newObject = storedObject{} if _, ok := io.Copy(&bytesBuffer, data); ok == nil { size := bytesBuffer.Len() etag := fmt.Sprintf("%x", sha256.Sum256(bytesBuffer.Bytes())) newObject.metadata = mstorage.ObjectMetadata{ + Bucket: bucket, Key: key, Created: time.Now(), Size: size, @@ -71,13 +76,13 @@ func (storage *storage) StoreBucket(bucketName string) error { if _, ok := storage.bucketdata[bucketName]; ok == true { return mstorage.BucketExists{Bucket: bucketName} } - newBucket := storedBucket{} - newBucket.metadata = mstorage.BucketMetadata{ - Name: bucketName, - Created: time.Now(), - } - log.Println(bucketName) + + var newBucket = storedBucket{} + newBucket.metadata = mstorage.BucketMetadata{} + newBucket.metadata.Name = bucketName + newBucket.metadata.Created = time.Now() storage.bucketdata[bucketName] = newBucket + return nil } @@ -85,8 +90,10 @@ func (storage *storage) ListObjects(bucket, prefix string, count int) []mstorage // TODO prefix and count handling var results []mstorage.ObjectMetadata for key, object := range storage.objectdata { - if strings.HasPrefix(key, bucket+":") { - results = append(results, object.metadata) + if bucket == object.metadata.Bucket { + if strings.HasPrefix(key, bucket+":") { + results = append(results, object.metadata) + } } } return results diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index 64000f826..8c69fbd9b 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -40,6 +40,7 @@ type BucketMetadata struct { } type ObjectMetadata struct { + Bucket string Key string Created time.Time Size int diff --git a/pkg/storage/storage_errors.go b/pkg/storage/storage_errors.go index a6ddfd865..7e3e142dc 100644 --- a/pkg/storage/storage_errors.go +++ b/pkg/storage/storage_errors.go @@ -10,16 +10,16 @@ type ObjectExists struct { Key string } -type BucketNameInvalid struct { - Bucket string -} - -type BucketExists struct { - Bucket string -} - type ObjectNotFound GenericError +type GenericBucketError struct { + Bucket string +} + +type BucketNameInvalid GenericBucketError +type BucketExists GenericBucketError +type BucketNotFound GenericBucketError + func (self ObjectNotFound) Error() string { return "Object not Found: " + self.Bucket + "#" + self.Path } @@ -35,3 +35,7 @@ func (self BucketNameInvalid) Error() string { func (self BucketExists) Error() string { return "Bucket exists: " + self.Bucket } + +func (self BucketNotFound) Error() string { + return "Bucket not Found: " + self.Bucket +} diff --git a/pkg/webapi/minioapi/minioapi.go b/pkg/webapi/minioapi/minioapi.go index 0d1cb222d..f46429f32 100644 --- a/pkg/webapi/minioapi/minioapi.go +++ b/pkg/webapi/minioapi/minioapi.go @@ -51,9 +51,8 @@ type encoder interface { func HttpHandler(storage mstorage.Storage) http.Handler { mux := mux.NewRouter() - api := minioApi{ - storage: storage, - } + var api = minioApi{} + api.storage = storage mux.HandleFunc("/", api.listBucketsHandler).Methods("GET") mux.HandleFunc("/{bucket}", api.listObjectsHandler).Methods("GET") @@ -62,6 +61,7 @@ func HttpHandler(storage mstorage.Storage) http.Handler { mux.HandleFunc("/{bucket}/{object:.*}", api.getObjectHandler).Methods("GET") mux.HandleFunc("/{bucket}/{object:.*}", api.headObjectHandler).Methods("HEAD") mux.HandleFunc("/{bucket}/{object:.*}", api.putObjectHandler).Methods("PUT") + return mux } @@ -113,9 +113,8 @@ func (server *minioApi) headObjectHandler(w http.ResponseWriter, req *http.Reque func (server *minioApi) listBucketsHandler(w http.ResponseWriter, req *http.Request) { vars := mux.Vars(req) - var prefix string - var ok bool - if prefix, ok = vars["prefix"]; ok == false { + prefix, ok := vars["prefix"] + if !ok { prefix = "" } @@ -145,7 +144,7 @@ func (server *minioApi) listObjectsHandler(w http.ResponseWriter, req *http.Requ vars := mux.Vars(req) bucket := vars["bucket"] prefix, ok := vars["prefix"] - if ok == false { + if !ok { prefix = "" } @@ -196,8 +195,7 @@ func (server *minioApi) putBucketHandler(w http.ResponseWriter, req *http.Reques } } -// Helpers - +// Write Object Header helper func writeObjectHeaders(w http.ResponseWriter, metadata mstorage.ObjectMetadata) { lastModified := metadata.Created.Format(time.RFC1123) w.Header().Set("ETag", metadata.ETag) @@ -206,53 +204,48 @@ func writeObjectHeaders(w http.ResponseWriter, metadata mstorage.ObjectMetadata) w.Header().Set("Content-Type", "text/plain") } -func generateBucketsListResult(buckets []mstorage.BucketMetadata) (data BucketListResponse) { +func generateBucketsListResult(buckets []mstorage.BucketMetadata) BucketListResponse { var listbuckets []*Bucket + var data = BucketListResponse{} + var owner = Owner{} - owner := Owner{ - ID: "minio", - DisplayName: "minio", - } + owner.ID = "minio" + owner.DisplayName = "minio" for _, bucket := range buckets { - listbucket := &Bucket{ - Name: bucket.Name, - CreationDate: bucket.Created.Format(dateFormat), - } + var listbucket = &Bucket{} + listbucket.Name = bucket.Name + listbucket.CreationDate = bucket.Created.Format(dateFormat) listbuckets = append(listbuckets, listbucket) } - data = BucketListResponse{ - Owner: owner, - } + data.Owner = owner data.Buckets.Bucket = listbuckets - return + + return data } -func generateObjectsListResult(bucket string, objects []mstorage.ObjectMetadata) (data ObjectListResponse) { +func generateObjectsListResult(bucket string, objects []mstorage.ObjectMetadata) ObjectListResponse { var contents []*Item + var owner = Owner{} + var data = ObjectListResponse{} - owner := Owner{ - ID: "minio", - DisplayName: "minio", - } + owner.ID = "minio" + owner.DisplayName = "minio" for _, object := range objects { - content := &Item{ - Key: object.Key, - LastModified: object.Created.Format(dateFormat), - ETag: object.ETag, - Size: object.Size, - StorageClass: "STANDARD", - Owner: owner, - } + var content = &Item{} + content.Key = object.Key + content.LastModified = object.Created.Format(dateFormat) + content.ETag = object.ETag + content.Size = object.Size + content.StorageClass = "STANDARD" + content.Owner = owner contents = append(contents, content) } - data = ObjectListResponse{ - Name: bucket, - Contents: contents, - MaxKeys: MAX_OBJECT_LIST, - IsTruncated: false, - } - return + data.Name = bucket + data.Contents = contents + data.MaxKeys = MAX_OBJECT_LIST + data.IsTruncated = false + return data }