s3manager-web/create_bucket.go

38 lines
817 B
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package s3manager
2017-03-30 22:48:27 +02:00
import (
"encoding/json"
"net/http"
minio "github.com/minio/minio-go"
2017-05-25 18:33:44 +02:00
"github.com/pkg/errors"
2017-03-30 22:48:27 +02:00
)
2017-05-08 23:07:07 +02:00
// CreateBucketHandler creates a new bucket.
func CreateBucketHandler(s3 S3) http.Handler {
2017-03-30 22:48:27 +02:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var bucket minio.BucketInfo
err := json.NewDecoder(r.Body).Decode(&bucket)
if err != nil {
2017-05-25 18:33:44 +02:00
handleHTTPError(w, errors.Wrap(err, errDecodingBody))
2017-03-30 22:48:27 +02:00
return
}
err = s3.MakeBucket(bucket.Name, "")
if err != nil {
2017-05-25 18:33:44 +02:00
handleHTTPError(w, errors.Wrap(err, "error making bucket"))
2017-03-30 22:48:27 +02:00
return
}
2017-05-08 23:07:07 +02:00
w.Header().Set(HeaderContentType, ContentTypeJSON)
2017-03-30 22:48:27 +02:00
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(bucket)
if err != nil {
2017-05-25 18:33:44 +02:00
handleHTTPError(w, errors.Wrap(err, errEncodingJSON))
2017-03-30 22:48:27 +02:00
return
}
})
}