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

36 lines
822 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"
2019-09-05 00:44:02 +02:00
"fmt"
2017-03-30 22:48:27 +02:00
"net/http"
minio "github.com/minio/minio-go"
)
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 {
2019-09-05 00:44:02 +02:00
handleHTTPError(w, fmt.Errorf("error decoding body JSON: %w", err))
2017-03-30 22:48:27 +02:00
return
}
err = s3.MakeBucket(bucket.Name, "")
if err != nil {
2019-09-05 00:44:02 +02:00
handleHTTPError(w, fmt.Errorf("error making bucket: %w", err))
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 {
2019-09-05 00:44:02 +02:00
handleHTTPError(w, fmt.Errorf("error encoding JSON: %w", err))
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
}