s3manager-web/internal/app/s3manager/create_bucket.go

36 lines
831 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
)
2018-05-31 16:10:41 +02:00
// HandleCreateBucket creates a new bucket.
func HandleCreateBucket(s3 S3) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2017-03-30 22:48:27 +02:00
var bucket minio.BucketInfo
err := json.NewDecoder(r.Body).Decode(&bucket)
if err != nil {
2018-05-22 22:56:01 +02:00
handleHTTPError(w, errors.Wrap(err, "error decoding body JSON"))
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
}
2018-05-22 22:56:01 +02:00
w.Header().Set("Content-Type", "application/json; charset=utf-8")
2017-03-30 22:48:27 +02:00
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(bucket)
if err != nil {
2018-05-22 22:56:01 +02:00
handleHTTPError(w, errors.Wrap(err, "error encoding JSON"))
2017-03-30 22:48:27 +02:00
return
}
2018-05-31 16:10:41 +02:00
}
2017-03-30 22:48:27 +02:00
}