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

37 lines
912 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 (
"net/http"
2018-05-31 16:10:41 +02:00
"github.com/matryer/way"
2017-12-05 23:30:28 +01:00
minio "github.com/minio/minio-go"
2017-05-25 18:33:44 +02:00
"github.com/pkg/errors"
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")
2017-04-14 19:52:16 +02:00
err := r.ParseMultipartForm(32 << 20)
if err != nil {
2018-05-22 22:56:01 +02:00
handleHTTPError(w, errors.Wrap(err, "error parsing multipart form"))
2017-04-14 19:52:16 +02:00
return
}
file, handler, err := r.FormFile("file")
if err != nil {
2017-05-25 18:33:44 +02:00
handleHTTPError(w, errors.Wrap(err, "error getting file from form"))
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-05-22 22:56:01 +02:00
_, err = s3.PutObject(bucketName, handler.Filename, file, 1, minio.PutObjectOptions{ContentType: "application/octet-stream"})
2017-04-14 19:52:16 +02:00
if err != nil {
2017-05-25 18:33:44 +02:00
handleHTTPError(w, errors.Wrap(err, "error putting object"))
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
}