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"
|
|
|
|
|
2021-08-05 11:44:40 +02:00
|
|
|
"github.com/minio/minio-go/v7"
|
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 {
|
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
|
|
|
|
}
|
|
|
|
|
2021-08-05 11:44:40 +02:00
|
|
|
err = s3.MakeBucket(r.Context(), bucket.Name, minio.MakeBucketOptions{})
|
2017-03-30 22:48:27 +02:00
|
|
|
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
|
|
|
}
|