From f8e2b75e230137f9f11d2ac01e09e8fe57f43722 Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Sat, 7 Mar 2015 19:10:55 -0800 Subject: [PATCH] Get object metadata needs to filter based of Prefix when delimiter is specified --- pkg/api/minioapi/api_test.go | 14 +++++++------- pkg/api/minioapi/object_handlers.go | 4 ++-- pkg/storage/fs/fs.go | 21 +++++++++++++-------- pkg/storage/inmemory/inmemory.go | 2 +- pkg/storage/storage.go | 2 +- pkg/storage/storage_api_suite.go | 9 +++++---- 6 files changed, 29 insertions(+), 23 deletions(-) diff --git a/pkg/api/minioapi/api_test.go b/pkg/api/minioapi/api_test.go index 74893e953..903edff69 100644 --- a/pkg/api/minioapi/api_test.go +++ b/pkg/api/minioapi/api_test.go @@ -71,7 +71,7 @@ func (s *MySuite) TestEmptyObject(c *C) { c.Assert(err, IsNil) c.Assert(true, Equals, bytes.Equal(responseBody, buffer.Bytes())) - metadata, err := storage.GetObjectMetadata("bucket", "object") + metadata, err := storage.GetObjectMetadata("bucket", "object", "") c.Assert(err, IsNil) verifyHeaders(c, response.Header, metadata.Created, 0, "application/octet-stream", metadata.ETag) @@ -96,7 +96,7 @@ func (s *MySuite) TestObject(c *C) { c.Assert(err, IsNil) c.Assert(true, Equals, bytes.Equal(responseBody, []byte("hello world"))) - metadata, err := storage.GetObjectMetadata("bucket", "object") + metadata, err := storage.GetObjectMetadata("bucket", "object", "") c.Assert(err, IsNil) verifyHeaders(c, response.Header, metadata.Created, len("hello world"), "application/octet-stream", metadata.ETag) } @@ -129,7 +129,7 @@ func (s *MySuite) TestMultipleObjects(c *C) { c.Assert(err, IsNil) // get metadata - metadata, err := storage.GetObjectMetadata("bucket", "object1") + metadata, err := storage.GetObjectMetadata("bucket", "object1", "") c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) @@ -148,7 +148,7 @@ func (s *MySuite) TestMultipleObjects(c *C) { c.Assert(err, IsNil) // get metadata - metadata, err = storage.GetObjectMetadata("bucket", "object2") + metadata, err = storage.GetObjectMetadata("bucket", "object2", "") c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) @@ -167,7 +167,7 @@ func (s *MySuite) TestMultipleObjects(c *C) { c.Assert(err, IsNil) // get metadata - metadata, err = storage.GetObjectMetadata("bucket", "object3") + metadata, err = storage.GetObjectMetadata("bucket", "object3", "") c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) @@ -210,7 +210,7 @@ func (s *MySuite) TestHeader(c *C) { c.Assert(err, IsNil) c.Assert(response.StatusCode, Equals, http.StatusOK) - metadata, err := storage.GetObjectMetadata("bucket", "object") + metadata, err := storage.GetObjectMetadata("bucket", "object", "") c.Assert(err, IsNil) verifyHeaders(c, response.Header, metadata.Created, len("hello world"), "application/octet-stream", metadata.ETag) } @@ -289,7 +289,7 @@ func (s *MySuite) TestPutObject(c *C) { c.Assert(bytes.Equal(writer.Bytes(), []byte("hello world")), Equals, true) - metadata, err := storage.GetObjectMetadata("bucket", "two") + metadata, err := storage.GetObjectMetadata("bucket", "two", "") c.Assert(err, IsNil) lastModified := metadata.Created diff --git a/pkg/api/minioapi/object_handlers.go b/pkg/api/minioapi/object_handlers.go index e9f3565ac..de3390969 100644 --- a/pkg/api/minioapi/object_handlers.go +++ b/pkg/api/minioapi/object_handlers.go @@ -36,7 +36,7 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques bucket = vars["bucket"] object = vars["object"] - metadata, err := server.storage.GetObjectMetadata(bucket, object) + metadata, err := server.storage.GetObjectMetadata(bucket, object, "") switch err := err.(type) { case nil: // success { @@ -89,7 +89,7 @@ func (server *minioAPI) headObjectHandler(w http.ResponseWriter, req *http.Reque bucket = vars["bucket"] object = vars["object"] - metadata, err := server.storage.GetObjectMetadata(bucket, object) + metadata, err := server.storage.GetObjectMetadata(bucket, object, "") switch err := err.(type) { case nil: writeObjectHeaders(w, metadata) diff --git a/pkg/storage/fs/fs.go b/pkg/storage/fs/fs.go index 0d217bad6..a5bee3ee3 100644 --- a/pkg/storage/fs/fs.go +++ b/pkg/storage/fs/fs.go @@ -261,7 +261,7 @@ func (storage *Storage) CopyObjectToWriter(w io.Writer, bucket string, object st } // GetObjectMetadata - HEAD object -func (storage *Storage) GetObjectMetadata(bucket, object string) (mstorage.ObjectMetadata, error) { +func (storage *Storage) GetObjectMetadata(bucket, object, prefix string) (mstorage.ObjectMetadata, error) { if mstorage.IsValidBucket(bucket) == false { return mstorage.ObjectMetadata{}, mstorage.BucketNameInvalid{Bucket: bucket} } @@ -291,7 +291,6 @@ func (storage *Storage) GetObjectMetadata(bucket, object string) (mstorage.Objec var deserializedMetadata Metadata decoder := gob.NewDecoder(file) err = decoder.Decode(&deserializedMetadata) - if err != nil { return mstorage.ObjectMetadata{}, mstorage.EmbedError(bucket, object, err) } @@ -306,9 +305,10 @@ func (storage *Storage) GetObjectMetadata(bucket, object string) (mstorage.Objec if len(deserializedMetadata.Md5sum) != 0 { etag = hex.EncodeToString(deserializedMetadata.Md5sum) } + trimmedObject := strings.TrimPrefix(object, prefix) metadata := mstorage.ObjectMetadata{ Bucket: bucket, - Key: path.Base(object), + Key: trimmedObject, Created: stat.ModTime(), Size: stat.Size(), ETag: etag, @@ -395,13 +395,15 @@ func (storage *Storage) ListObjects(bucket string, resources mstorage.BucketReso delimitedName := delimiter(name, resources.Delimiter) switch true { case delimitedName == "": - metadata, err := storage.GetObjectMetadata(bucket, name) + // Do not strip prefix object output + metadata, err := storage.GetObjectMetadata(bucket, name, "") if err != nil { return []mstorage.ObjectMetadata{}, resources, mstorage.EmbedError(bucket, "", err) } metadataList = append(metadataList, metadata) case delimitedName == file.Name(): - metadata, err := storage.GetObjectMetadata(bucket, name) + // Do not strip prefix object output + metadata, err := storage.GetObjectMetadata(bucket, name, "") if err != nil { return []mstorage.ObjectMetadata{}, resources, mstorage.EmbedError(bucket, "", err) } @@ -414,13 +416,15 @@ func (storage *Storage) ListObjects(bucket string, resources mstorage.BucketReso delimitedName := delimiter(trimmedName, resources.Delimiter) switch true { case name == resources.Prefix: - metadata, err := storage.GetObjectMetadata(bucket, name) + // Use resources.Prefix to filter out delimited files + metadata, err := storage.GetObjectMetadata(bucket, name, resources.Prefix) if err != nil { return []mstorage.ObjectMetadata{}, resources, mstorage.EmbedError(bucket, "", err) } metadataList = append(metadataList, metadata) case delimitedName == file.Name(): - metadata, err := storage.GetObjectMetadata(bucket, name) + // Use resources.Prefix to filter out delimited files + metadata, err := storage.GetObjectMetadata(bucket, name, resources.Prefix) if err != nil { return []mstorage.ObjectMetadata{}, resources, mstorage.EmbedError(bucket, "", err) } @@ -433,7 +437,8 @@ func (storage *Storage) ListObjects(bucket string, resources mstorage.BucketReso } } case strings.HasPrefix(name, resources.Prefix): - metadata, err := storage.GetObjectMetadata(bucket, name) + // Do not strip prefix object output + metadata, err := storage.GetObjectMetadata(bucket, name, "") if err != nil { return []mstorage.ObjectMetadata{}, resources, mstorage.EmbedError(bucket, "", err) } diff --git a/pkg/storage/inmemory/inmemory.go b/pkg/storage/inmemory/inmemory.go index 6373b7112..71b2725bc 100644 --- a/pkg/storage/inmemory/inmemory.go +++ b/pkg/storage/inmemory/inmemory.go @@ -198,7 +198,7 @@ func (storage *Storage) ListBuckets() ([]mstorage.BucketMetadata, error) { } // GetObjectMetadata - get object metadata from memory -func (storage *Storage) GetObjectMetadata(bucket, key string) (mstorage.ObjectMetadata, error) { +func (storage *Storage) GetObjectMetadata(bucket, key, prefix string) (mstorage.ObjectMetadata, error) { objectKey := bucket + ":" + key if object, ok := storage.objectdata[objectKey]; ok == true { diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index 18652baab..ec04fd0cf 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -33,7 +33,7 @@ type Storage interface { // Object Operations CopyObjectToWriter(w io.Writer, bucket string, object string) (int64, error) - GetObjectMetadata(bucket string, object string) (ObjectMetadata, error) + GetObjectMetadata(bucket string, object string, prefix string) (ObjectMetadata, error) ListObjects(bucket string, resources BucketResourcesMetadata) ([]ObjectMetadata, BucketResourcesMetadata, error) StoreObject(bucket string, key string, contentType string, data io.Reader) error } diff --git a/pkg/storage/storage_api_suite.go b/pkg/storage/storage_api_suite.go index c9e44db13..8c8254e5b 100644 --- a/pkg/storage/storage_api_suite.go +++ b/pkg/storage/storage_api_suite.go @@ -69,7 +69,7 @@ func testMultipleObjectCreation(c *check.C, create func() Storage) { storage.CopyObjectToWriter(&byteBuffer, "bucket", key) c.Assert(bytes.Equal(value, byteBuffer.Bytes()), check.Equals, true) - metadata, err := storage.GetObjectMetadata("bucket", key) + metadata, err := storage.GetObjectMetadata("bucket", key, "") c.Assert(err, check.IsNil) c.Assert(metadata.Size, check.Equals, int64(len(value))) @@ -133,6 +133,7 @@ func testPaging(c *check.C, create func() Storage) { resources.Prefix = "obj" resources.Maxkeys = 1000 objects, resources, err = storage.ListObjects("bucket", resources) + c.Log(objects) c.Assert(objects[0].Key, check.Equals, "obj0") c.Assert(objects[1].Key, check.Equals, "obj1") c.Assert(objects[2].Key, check.Equals, "obj10") @@ -320,19 +321,19 @@ func testDefaultContentType(c *check.C, create func() Storage) { // test empty err = storage.StoreObject("bucket", "one", "", bytes.NewBufferString("one")) - metadata, err := storage.GetObjectMetadata("bucket", "one") + metadata, err := storage.GetObjectMetadata("bucket", "one", "") c.Assert(err, check.IsNil) c.Assert(metadata.ContentType, check.Equals, "application/octet-stream") // test custom storage.StoreObject("bucket", "two", "application/text", bytes.NewBufferString("two")) - metadata, err = storage.GetObjectMetadata("bucket", "two") + metadata, err = storage.GetObjectMetadata("bucket", "two", "") c.Assert(err, check.IsNil) c.Assert(metadata.ContentType, check.Equals, "application/text") // test trim space storage.StoreObject("bucket", "three", "\tapplication/json ", bytes.NewBufferString("three")) - metadata, err = storage.GetObjectMetadata("bucket", "three") + metadata, err = storage.GetObjectMetadata("bucket", "three", "") c.Assert(err, check.IsNil) c.Assert(metadata.ContentType, check.Equals, "application/json") }