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

38 lines
920 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 (
2019-09-05 00:44:02 +02:00
"fmt"
2017-03-30 22:48:27 +02:00
"net/http"
2018-05-31 16:10:41 +02:00
"github.com/matryer/way"
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
// HandleCreateObject uploads a new object.
func HandleCreateObject(s3 S3) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
bucketName := way.Param(r.Context(), "bucketName")
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")
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
}
2018-05-31 18:28:55 +02:00
defer file.Close()
2017-03-30 22:48:27 +02:00
2018-06-23 12:49:44 +02:00
opts := minio.PutObjectOptions{ContentType: "application/octet-stream"}
2021-08-05 11:44:40 +02:00
_, err = s3.PutObject(r.Context(), bucketName, header.Filename, 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
}