Implement proper delimiter and prefix handling

With this change Minio server now responds with, delimited
'object names' in conjunction with prefix filtering

~~~
<ListBucketResult>
  <Name>example-bucket</Name>
  <Prefix></Prefix>
  <Marker></Marker>
  <MaxKeys>1000</MaxKeys>
  <Delimiter>/</Delimiter>
  <IsTruncated>false</IsTruncated>
  <Contents>
    <Key>sample.html</Key>
    <LastModified>2011-02-26T01:56:20.000Z</LastModified>
    <ETag>example-bucket#sample.html</ETag>
    <Size>142863</Size>
    <Owner>
      <ID>minio</ID>
      <DisplayName>minio</DisplayName>
    </Owner>
    <StorageClass>STANDARD</StorageClass>
  </Contents>
  <CommonPrefixes>
    <Prefix>photos/</Prefix>
  </CommonPrefixes>
</ListBucketResult>
~~~

~~~
<ListBucketResult>
  <Name>example-bucket</Name>
  <Prefix>photos/2006/</Prefix>
  <Marker></Marker>
  <MaxKeys>1000</MaxKeys>
  <Delimiter>/</Delimiter>
  <IsTruncated>false</IsTruncated>

  <CommonPrefixes>
    <Prefix>photos/2006/feb/</Prefix>
  </CommonPrefixes>
  <CommonPrefixes>
    <Prefix>photos/2006/jan/</Prefix>
  </CommonPrefixes>
</ListBucketResult>
~~~
This commit is contained in:
Harshavardhana 2015-02-28 14:44:26 -08:00
parent acee4d16fd
commit 0c2d58bc6d
5 changed files with 84 additions and 37 deletions

View File

@ -45,7 +45,7 @@ func (server *minioApi) listObjectsHandler(w http.ResponseWriter, req *http.Requ
switch err := err.(type) {
case nil: // success
{
response := generateObjectsListResult(bucket, objects, resources.IsTruncated)
response := generateObjectsListResult(bucket, objects, resources)
w.Write(writeObjectHeadersAndResponse(w, response, acceptsContentType))
}
case mstorage.BucketNotFound:

View File

@ -35,9 +35,7 @@ type ObjectListResponse struct {
Delimiter string
IsTruncated bool
Contents []*Item `xml:,innerxml`
CommonPrefixes struct {
Prefix string
} `xml:,innerxml`
CommonPrefixes []*Prefix `xml:,innerxml`
}
// Bucket list response format
@ -49,6 +47,10 @@ type BucketListResponse struct {
} `xml:,innerxml` // Buckets are nested
}
type Prefix struct {
Prefix string
}
// Bucket struct
type Bucket struct {
Name string

View File

@ -68,8 +68,9 @@ func (b ItemKey) Less(i, j int) bool { return b[i].Key < b[j].Key }
//
// output:
// populated struct that can be serialized to match xml and json api spec output
func generateObjectsListResult(bucket string, objects []mstorage.ObjectMetadata, isTruncated bool) ObjectListResponse {
func generateObjectsListResult(bucket string, objects []mstorage.ObjectMetadata, bucketResources mstorage.BucketResourcesMetadata) ObjectListResponse {
var contents []*Item
var prefixes []*Prefix
var owner = Owner{}
var data = ObjectListResponse{}
@ -78,6 +79,9 @@ func generateObjectsListResult(bucket string, objects []mstorage.ObjectMetadata,
for _, object := range objects {
var content = &Item{}
if object.Key == "" {
continue
}
content.Key = object.Key
content.LastModified = object.Created.Format(dateFormat)
content.ETag = object.ETag
@ -89,7 +93,16 @@ func generateObjectsListResult(bucket string, objects []mstorage.ObjectMetadata,
sort.Sort(ItemKey(contents))
data.Name = bucket
data.Contents = contents
data.MaxKeys = MAX_OBJECT_LIST
data.IsTruncated = isTruncated
data.MaxKeys = bucketResources.Maxkeys
data.Prefix = bucketResources.Prefix
data.Delimiter = bucketResources.Delimiter
data.Marker = bucketResources.Marker
data.IsTruncated = bucketResources.IsTruncated
for _, prefix := range bucketResources.CommonPrefixes {
var prefixItem = &Prefix{}
prefixItem.Prefix = prefix
prefixes = append(prefixes, prefixItem)
}
data.CommonPrefixes = prefixes
return data
}

View File

@ -314,6 +314,29 @@ func (p *Path) getAllFiles(path string, fl os.FileInfo, err error) error {
return nil
}
func delimiter(path, delimiter string) string {
delimited := ""
if !strings.Contains(path, delimiter) {
return delimited
}
index := strings.Index(path, delimiter)
if index == -1 {
return delimited
}
delimitedIndex := index + len(delimiter)
delimited = path[:delimitedIndex]
return delimited
}
func appendU(slice []string, i string) []string {
for _, ele := range slice {
if ele == i {
return slice
}
}
return append(slice, i)
}
type ByObjectKey []mstorage.ObjectMetadata
func (b ByObjectKey) Len() int { return len(b) }
@ -350,27 +373,40 @@ func (storage *storage) ListObjects(bucket string, resources mstorage.BucketReso
goto ret
}
// TODO handle resources.Marker
if resources.Delimiter != "" {
switch true {
case resources.Delimiter != "" && resources.Prefix == "":
delimited := delimiter(name, resources.Delimiter)
switch true {
case delimited == "":
metadata := mstorage.ObjectMetadata{
Bucket: bucket,
Maxkeys: resources.Maxkeys,
Prefix: resources.Prefix,
Marker: resources.Marker,
Delimiter: resources.Delimiter,
Key: name,
Created: file.ModTime(),
Size: file.Size(),
ETag: bucket + "#" + name,
}
metadataList = append(metadataList, metadata)
case delimited != "":
resources.CommonPrefixes = appendU(resources.CommonPrefixes, delimited)
}
if resources.Delimiter != "" && strings.HasPrefix(name, resources.Prefix) {
metadata := mstorage.ObjectMetadata{}
metadataList = append(metadataList, metadata)
}
if strings.HasPrefix(name, resources.Prefix) {
case resources.Delimiter != "" && strings.HasPrefix(name, resources.Prefix):
delimited := delimiter(name, resources.Delimiter)
switch true {
case delimited == "":
metadata := mstorage.ObjectMetadata{
Bucket: bucket,
Key: name,
Created: file.ModTime(),
Size: file.Size(),
ETag: bucket + "#" + name,
}
metadataList = append(metadataList, metadata)
case delimited != "":
resources.CommonPrefixes = appendU(resources.CommonPrefixes, delimited)
}
case strings.HasPrefix(name, resources.Prefix):
metadata := mstorage.ObjectMetadata{
Bucket: bucket,
Maxkeys: resources.Maxkeys,
Prefix: resources.Prefix,
Marker: resources.Marker,
Delimiter: resources.Delimiter,
Key: name,
Created: file.ModTime(),
Size: file.Size(),

View File

@ -45,10 +45,6 @@ type BucketMetadata struct {
type ObjectMetadata struct {
Bucket string
Key string
Maxkeys int
Prefix string
Marker string
Delimiter string
ContentType string
Created time.Time