2017-05-08 23:07:07 +02:00
|
|
|
package s3manager
|
2017-03-30 22:48:27 +02:00
|
|
|
|
|
|
|
import (
|
2019-09-05 00:44:02 +02:00
|
|
|
"fmt"
|
2022-03-23 10:10:30 +01:00
|
|
|
"log"
|
|
|
|
"mime/multipart"
|
2017-03-30 22:48:27 +02:00
|
|
|
"net/http"
|
|
|
|
|
2022-03-23 10:10:30 +01:00
|
|
|
"github.com/gorilla/mux"
|
2021-08-05 11:44:40 +02:00
|
|
|
"github.com/minio/minio-go/v7"
|
2023-02-09 12:05:14 +01:00
|
|
|
"github.com/minio/minio-go/v7/pkg/encrypt"
|
2017-03-30 22:48:27 +02:00
|
|
|
)
|
|
|
|
|
2018-05-31 16:10:41 +02:00
|
|
|
// HandleCreateObject uploads a new object.
|
2023-02-09 12:05:14 +01:00
|
|
|
func HandleCreateObject(s3 S3, sseInfo SSEType) http.HandlerFunc {
|
2018-05-31 16:10:41 +02:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-03-23 10:10:30 +01:00
|
|
|
bucketName := mux.Vars(r)["bucketName"]
|
2018-05-31 16:10:41 +02:00
|
|
|
|
2019-09-05 00:44:02 +02:00
|
|
|
err := r.ParseMultipartForm(0)
|
2017-04-14 19:52:16 +02:00
|
|
|
if err != nil {
|
2019-09-05 00:44:02 +02:00
|
|
|
handleHTTPError(w, fmt.Errorf("error parsing multipart form: %w", err))
|
2017-04-14 19:52:16 +02:00
|
|
|
return
|
|
|
|
}
|
2020-02-27 09:22:14 +01:00
|
|
|
file, header, err := r.FormFile("file")
|
2023-06-20 07:18:56 +02:00
|
|
|
path := r.FormValue("path")
|
2017-04-14 19:52:16 +02:00
|
|
|
if err != nil {
|
2019-09-05 00:44:02 +02:00
|
|
|
handleHTTPError(w, fmt.Errorf("error getting file from form: %w", err))
|
2017-04-14 19:52:16 +02:00
|
|
|
return
|
|
|
|
}
|
2022-03-23 10:10:30 +01:00
|
|
|
defer func(file multipart.File) {
|
2022-03-24 08:46:21 +01:00
|
|
|
if err = file.Close(); err != nil {
|
2022-03-23 10:10:30 +01:00
|
|
|
log.Fatal(fmt.Errorf("file cannot be closed: %w", err))
|
|
|
|
}
|
|
|
|
}(file)
|
2017-03-30 22:48:27 +02:00
|
|
|
|
2018-06-23 12:49:44 +02:00
|
|
|
opts := minio.PutObjectOptions{ContentType: "application/octet-stream"}
|
2023-02-09 12:05:14 +01:00
|
|
|
|
|
|
|
if sseInfo.Type == "KMS" {
|
|
|
|
opts.ServerSideEncryption, _ = encrypt.NewSSEKMS(sseInfo.Key, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sseInfo.Type == "SSE" {
|
|
|
|
opts.ServerSideEncryption = encrypt.NewSSE()
|
|
|
|
}
|
|
|
|
|
|
|
|
if sseInfo.Type == "SSE-C" {
|
|
|
|
opts.ServerSideEncryption, err = encrypt.NewSSEC([]byte(sseInfo.Key))
|
|
|
|
if err != nil {
|
|
|
|
handleHTTPError(w, fmt.Errorf("error setting SSE-C key: %w", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-20 07:18:56 +02:00
|
|
|
objectName := fmt.Sprintf("%s%s", path, header.Filename)
|
|
|
|
_, err = s3.PutObject(r.Context(), bucketName, objectName, file, -1, opts)
|
2017-04-14 19:52:16 +02:00
|
|
|
if err != nil {
|
2019-09-05 00:44:02 +02:00
|
|
|
handleHTTPError(w, fmt.Errorf("error putting object: %w", err))
|
2017-04-14 19:52:16 +02:00
|
|
|
return
|
2017-03-30 22:48:27 +02:00
|
|
|
}
|
2017-04-14 19:52:16 +02:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
2018-05-31 16:10:41 +02:00
|
|
|
}
|
2017-03-30 22:48:27 +02:00
|
|
|
}
|