Get object metadata needs to filter based of Prefix when delimiter is specified

master
Harshavardhana 10 years ago
parent df69038b04
commit f8e2b75e23
  1. 14
      pkg/api/minioapi/api_test.go
  2. 4
      pkg/api/minioapi/object_handlers.go
  3. 21
      pkg/storage/fs/fs.go
  4. 2
      pkg/storage/inmemory/inmemory.go
  5. 2
      pkg/storage/storage.go
  6. 9
      pkg/storage/storage_api_suite.go

@ -71,7 +71,7 @@ func (s *MySuite) TestEmptyObject(c *C) {
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(true, Equals, bytes.Equal(responseBody, buffer.Bytes())) 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) c.Assert(err, IsNil)
verifyHeaders(c, response.Header, metadata.Created, 0, "application/octet-stream", metadata.ETag) 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(err, IsNil)
c.Assert(true, Equals, bytes.Equal(responseBody, []byte("hello world"))) 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) c.Assert(err, IsNil)
verifyHeaders(c, response.Header, metadata.Created, len("hello world"), "application/octet-stream", metadata.ETag) 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) c.Assert(err, IsNil)
// get metadata // get metadata
metadata, err := storage.GetObjectMetadata("bucket", "object1") metadata, err := storage.GetObjectMetadata("bucket", "object1", "")
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(response.StatusCode, Equals, http.StatusOK) c.Assert(response.StatusCode, Equals, http.StatusOK)
@ -148,7 +148,7 @@ func (s *MySuite) TestMultipleObjects(c *C) {
c.Assert(err, IsNil) c.Assert(err, IsNil)
// get metadata // get metadata
metadata, err = storage.GetObjectMetadata("bucket", "object2") metadata, err = storage.GetObjectMetadata("bucket", "object2", "")
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(response.StatusCode, Equals, http.StatusOK) c.Assert(response.StatusCode, Equals, http.StatusOK)
@ -167,7 +167,7 @@ func (s *MySuite) TestMultipleObjects(c *C) {
c.Assert(err, IsNil) c.Assert(err, IsNil)
// get metadata // get metadata
metadata, err = storage.GetObjectMetadata("bucket", "object3") metadata, err = storage.GetObjectMetadata("bucket", "object3", "")
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(response.StatusCode, Equals, http.StatusOK) c.Assert(response.StatusCode, Equals, http.StatusOK)
@ -210,7 +210,7 @@ func (s *MySuite) TestHeader(c *C) {
c.Assert(err, IsNil) c.Assert(err, IsNil)
c.Assert(response.StatusCode, Equals, http.StatusOK) c.Assert(response.StatusCode, Equals, http.StatusOK)
metadata, err := storage.GetObjectMetadata("bucket", "object") metadata, err := storage.GetObjectMetadata("bucket", "object", "")
c.Assert(err, IsNil) c.Assert(err, IsNil)
verifyHeaders(c, response.Header, metadata.Created, len("hello world"), "application/octet-stream", metadata.ETag) 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) 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) c.Assert(err, IsNil)
lastModified := metadata.Created lastModified := metadata.Created

@ -36,7 +36,7 @@ func (server *minioAPI) getObjectHandler(w http.ResponseWriter, req *http.Reques
bucket = vars["bucket"] bucket = vars["bucket"]
object = vars["object"] object = vars["object"]
metadata, err := server.storage.GetObjectMetadata(bucket, object) metadata, err := server.storage.GetObjectMetadata(bucket, object, "")
switch err := err.(type) { switch err := err.(type) {
case nil: // success case nil: // success
{ {
@ -89,7 +89,7 @@ func (server *minioAPI) headObjectHandler(w http.ResponseWriter, req *http.Reque
bucket = vars["bucket"] bucket = vars["bucket"]
object = vars["object"] object = vars["object"]
metadata, err := server.storage.GetObjectMetadata(bucket, object) metadata, err := server.storage.GetObjectMetadata(bucket, object, "")
switch err := err.(type) { switch err := err.(type) {
case nil: case nil:
writeObjectHeaders(w, metadata) writeObjectHeaders(w, metadata)

@ -261,7 +261,7 @@ func (storage *Storage) CopyObjectToWriter(w io.Writer, bucket string, object st
} }
// GetObjectMetadata - HEAD object // 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 { if mstorage.IsValidBucket(bucket) == false {
return mstorage.ObjectMetadata{}, mstorage.BucketNameInvalid{Bucket: bucket} return mstorage.ObjectMetadata{}, mstorage.BucketNameInvalid{Bucket: bucket}
} }
@ -291,7 +291,6 @@ func (storage *Storage) GetObjectMetadata(bucket, object string) (mstorage.Objec
var deserializedMetadata Metadata var deserializedMetadata Metadata
decoder := gob.NewDecoder(file) decoder := gob.NewDecoder(file)
err = decoder.Decode(&deserializedMetadata) err = decoder.Decode(&deserializedMetadata)
if err != nil { if err != nil {
return mstorage.ObjectMetadata{}, mstorage.EmbedError(bucket, object, err) 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 { if len(deserializedMetadata.Md5sum) != 0 {
etag = hex.EncodeToString(deserializedMetadata.Md5sum) etag = hex.EncodeToString(deserializedMetadata.Md5sum)
} }
trimmedObject := strings.TrimPrefix(object, prefix)
metadata := mstorage.ObjectMetadata{ metadata := mstorage.ObjectMetadata{
Bucket: bucket, Bucket: bucket,
Key: path.Base(object), Key: trimmedObject,
Created: stat.ModTime(), Created: stat.ModTime(),
Size: stat.Size(), Size: stat.Size(),
ETag: etag, ETag: etag,
@ -395,13 +395,15 @@ func (storage *Storage) ListObjects(bucket string, resources mstorage.BucketReso
delimitedName := delimiter(name, resources.Delimiter) delimitedName := delimiter(name, resources.Delimiter)
switch true { switch true {
case delimitedName == "": case delimitedName == "":
metadata, err := storage.GetObjectMetadata(bucket, name) // Do not strip prefix object output
metadata, err := storage.GetObjectMetadata(bucket, name, "")
if err != nil { if err != nil {
return []mstorage.ObjectMetadata{}, resources, mstorage.EmbedError(bucket, "", err) return []mstorage.ObjectMetadata{}, resources, mstorage.EmbedError(bucket, "", err)
} }
metadataList = append(metadataList, metadata) metadataList = append(metadataList, metadata)
case delimitedName == file.Name(): 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 { if err != nil {
return []mstorage.ObjectMetadata{}, resources, mstorage.EmbedError(bucket, "", err) 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) delimitedName := delimiter(trimmedName, resources.Delimiter)
switch true { switch true {
case name == resources.Prefix: 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 { if err != nil {
return []mstorage.ObjectMetadata{}, resources, mstorage.EmbedError(bucket, "", err) return []mstorage.ObjectMetadata{}, resources, mstorage.EmbedError(bucket, "", err)
} }
metadataList = append(metadataList, metadata) metadataList = append(metadataList, metadata)
case delimitedName == file.Name(): 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 { if err != nil {
return []mstorage.ObjectMetadata{}, resources, mstorage.EmbedError(bucket, "", err) 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): 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 { if err != nil {
return []mstorage.ObjectMetadata{}, resources, mstorage.EmbedError(bucket, "", err) return []mstorage.ObjectMetadata{}, resources, mstorage.EmbedError(bucket, "", err)
} }

@ -198,7 +198,7 @@ func (storage *Storage) ListBuckets() ([]mstorage.BucketMetadata, error) {
} }
// GetObjectMetadata - get object metadata from memory // 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 objectKey := bucket + ":" + key
if object, ok := storage.objectdata[objectKey]; ok == true { if object, ok := storage.objectdata[objectKey]; ok == true {

@ -33,7 +33,7 @@ type Storage interface {
// Object Operations // Object Operations
CopyObjectToWriter(w io.Writer, bucket string, object string) (int64, error) 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) ListObjects(bucket string, resources BucketResourcesMetadata) ([]ObjectMetadata, BucketResourcesMetadata, error)
StoreObject(bucket string, key string, contentType string, data io.Reader) error StoreObject(bucket string, key string, contentType string, data io.Reader) error
} }

@ -69,7 +69,7 @@ func testMultipleObjectCreation(c *check.C, create func() Storage) {
storage.CopyObjectToWriter(&byteBuffer, "bucket", key) storage.CopyObjectToWriter(&byteBuffer, "bucket", key)
c.Assert(bytes.Equal(value, byteBuffer.Bytes()), check.Equals, true) 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(err, check.IsNil)
c.Assert(metadata.Size, check.Equals, int64(len(value))) 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.Prefix = "obj"
resources.Maxkeys = 1000 resources.Maxkeys = 1000
objects, resources, err = storage.ListObjects("bucket", resources) objects, resources, err = storage.ListObjects("bucket", resources)
c.Log(objects)
c.Assert(objects[0].Key, check.Equals, "obj0") c.Assert(objects[0].Key, check.Equals, "obj0")
c.Assert(objects[1].Key, check.Equals, "obj1") c.Assert(objects[1].Key, check.Equals, "obj1")
c.Assert(objects[2].Key, check.Equals, "obj10") c.Assert(objects[2].Key, check.Equals, "obj10")
@ -320,19 +321,19 @@ func testDefaultContentType(c *check.C, create func() Storage) {
// test empty // test empty
err = storage.StoreObject("bucket", "one", "", bytes.NewBufferString("one")) 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(err, check.IsNil)
c.Assert(metadata.ContentType, check.Equals, "application/octet-stream") c.Assert(metadata.ContentType, check.Equals, "application/octet-stream")
// test custom // test custom
storage.StoreObject("bucket", "two", "application/text", bytes.NewBufferString("two")) 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(err, check.IsNil)
c.Assert(metadata.ContentType, check.Equals, "application/text") c.Assert(metadata.ContentType, check.Equals, "application/text")
// test trim space // test trim space
storage.StoreObject("bucket", "three", "\tapplication/json ", bytes.NewBufferString("three")) 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(err, check.IsNil)
c.Assert(metadata.ContentType, check.Equals, "application/json") c.Assert(metadata.ContentType, check.Equals, "application/json")
} }

Loading…
Cancel
Save