s3manager-web/create-bucket.go
2017-04-07 10:07:23 +02:00

39 lines
903 B
Go

package main
import (
"encoding/json"
"net/http"
minio "github.com/minio/minio-go"
)
// CreateBucketHandler creates a new bucket
func CreateBucketHandler(s3 S3Client) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var bucket minio.BucketInfo
err := json.NewDecoder(r.Body).Decode(&bucket)
if err != nil {
msg := "error decoding json"
handleHTTPError(w, msg, err, http.StatusUnprocessableEntity)
return
}
err = s3.MakeBucket(bucket.Name, "")
if err != nil {
msg := "error making bucket"
handleHTTPError(w, msg, err, http.StatusInternalServerError)
return
}
w.Header().Set(headerContentType, contentTypeJSON)
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(bucket)
if err != nil {
msg := "error encoding json"
handleHTTPError(w, msg, err, http.StatusInternalServerError)
return
}
})
}