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

63 lines
1.5 KiB
Go
Raw Normal View History

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"
"log"
"mime/multipart"
2017-03-30 22:48:27 +02:00
"net/http"
"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) {
bucketName := mux.Vars(r)["bucketName"]
2018-05-31 16:10:41 +02:00
err := r.ParseMultipartForm(32 << 20) // 32 Mb
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
}
2023-07-16 15:09:13 +02:00
file, _, 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
}
defer func(file multipart.File) {
2022-03-24 08:46:21 +01:00
if err = file.Close(); err != nil {
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-07-16 15:09:13 +02:00
_, err = s3.PutObject(r.Context(), bucketName, path, 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
}