List objects in a bucket.

This commit is contained in:
Frederick F. Kautz IV
2015-01-20 18:39:30 -08:00
parent d6fd1b0a38
commit cc4329fb12
3 changed files with 91 additions and 39 deletions

View File

@@ -7,9 +7,9 @@ import (
type ListResponse struct {
XMLName xml.Name `xml:"ListBucketResult"`
Name string `xml:"Name"`
Prefix string
toragerefix string
Marker string
MaxKeys int32
MaxKeys int
IsTruncated bool
Contents []Content `xml:"Contents",innerxml`
}
@@ -18,7 +18,7 @@ type Content struct {
Key string
LastModified string
ETag string
Size uint64
Size int
StorageClass string
Owner Owner
}

View File

@@ -2,9 +2,11 @@ package minioapi
import (
"bytes"
"encoding/json"
"encoding/xml"
"log"
"net/http"
"time"
"github.com/gorilla/mux"
mstorage "github.com/minio-io/minio/pkg/storage"
@@ -14,12 +16,17 @@ type minioApi struct {
storage *mstorage.Storage
}
type encoder interface {
Encode(v interface{}) error
}
func HttpHandler(storage *mstorage.Storage) http.Handler {
mux := mux.NewRouter()
api := minioApi{
storage: storage,
}
mux.HandleFunc("/", api.listHandler).Methods("GET")
mux.HandleFunc("/", api.listBucketsHandler).Methods("GET")
mux.HandleFunc("/{bucket}/", api.listObjectsHandler).Methods("GET")
mux.HandleFunc("/{bucket}/{object:.*}", api.getObjectHandler).Methods("GET")
mux.HandleFunc("/{bucket}/{object:.*}", api.putObjectHandler).Methods("PUT")
return mux
@@ -49,14 +56,18 @@ func (server *minioApi) getObjectHandler(w http.ResponseWriter, req *http.Reques
}
}
func (server *minioApi) listHandler(w http.ResponseWriter, req *http.Request) {
func (server *minioApi) listBucketsHandler(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("/"))
}
func (server *minioApi) listObjectsHandler(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req)
//delimiter, ok := vars["delimiter"]
//encodingType, ok := vars["encoding-type"]
//marker, ok := vars["marker"]
//maxKeys, ok := vars["max-keys"]
bucket := "bucket"
bucket := vars["bucket"]
//bucket, ok := vars["bucket"]
//if ok == false {
// w.WriteHeader(http.StatusBadRequest)
@@ -67,14 +78,26 @@ func (server *minioApi) listHandler(w http.ResponseWriter, req *http.Request) {
prefix = ""
}
contentType := "xml"
if req.Header["Accept"][0] == "application/json" {
contentType = "json"
}
objects := server.storage.ListObjects(bucket, prefix, 1000)
response := generateListResult(objects)
var bytesBuffer bytes.Buffer
xmlEncoder := xml.NewEncoder(&bytesBuffer)
xmlEncoder.Encode(response)
var encoder encoder
if contentType == "json" {
w.Header().Set("Content-Type", "application/json")
encoder = json.NewEncoder(&bytesBuffer)
} else {
w.Header().Set("Content-Type", "application/xml")
encoder = xml.NewEncoder(&bytesBuffer)
}
encoder.Encode(response)
w.Header().Set("Content-Type", "application/xml")
w.Write(bytesBuffer.Bytes())
}
@@ -86,31 +109,34 @@ func (server *minioApi) putObjectHandler(w http.ResponseWriter, req *http.Reques
}
func generateListResult(objects []mstorage.ObjectMetadata) ListResponse {
contents := []Content{}
owner := Owner{
ID: "MyID",
DisplayName: "MyDisplayName",
ID: "minio",
DisplayName: "minio",
}
contents := []Content{
Content{
Key: "one",
LastModified: "two",
ETag: "\"ETag\"",
Size: 1,
StorageClass: "three",
for _, object := range objects {
content := Content{
Key: object.Key,
LastModified: formatDate(object.SecCreated),
ETag: object.Key,
Size: object.Size,
StorageClass: "STANDARD",
Owner: owner,
},
Content{
Key: "four",
LastModified: "five",
ETag: "\"ETag\"",
Size: 1,
StorageClass: "six",
Owner: owner,
},
}
contents = append(contents, content)
}
data := &ListResponse{
Name: "name",
Contents: contents,
data := ListResponse{
Name: "name",
Contents: contents,
MaxKeys: len(objects),
IsTruncated: false,
}
return *data
return data
}
func formatDate(sec int64) string {
timeStamp := time.Unix(sec, 0)
return timeStamp.Format("2006-01-02T15:04:05.000Z")
}