|
|
|
@ -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.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", |
|
|
|
|
Owner: owner, |
|
|
|
|
}, |
|
|
|
|
Content{ |
|
|
|
|
Key: "four", |
|
|
|
|
LastModified: "five", |
|
|
|
|
ETag: "\"ETag\"", |
|
|
|
|
Size: 1, |
|
|
|
|
StorageClass: "six", |
|
|
|
|
|
|
|
|
|
for _, object := range objects { |
|
|
|
|
content := Content{ |
|
|
|
|
Key: object.Key, |
|
|
|
|
LastModified: formatDate(object.SecCreated), |
|
|
|
|
ETag: object.Key, |
|
|
|
|
Size: object.Size, |
|
|
|
|
StorageClass: "STANDARD", |
|
|
|
|
Owner: owner, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
data := &ListResponse{ |
|
|
|
|
contents = append(contents, content) |
|
|
|
|
} |
|
|
|
|
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") |
|
|
|
|
} |
|
|
|
|