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