2017-05-08 23:07:07 +02:00
|
|
|
package s3manager
|
2017-03-30 22:48:27 +02:00
|
|
|
|
|
|
|
import (
|
2017-04-19 14:18:58 +02:00
|
|
|
"log"
|
2017-03-30 22:48:27 +02:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
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
|
|
|
)
|
|
|
|
|
2017-05-08 23:07:07 +02:00
|
|
|
// CreateObjectHandler uploads a new object.
|
|
|
|
func CreateObjectHandler(s3 S3) http.Handler {
|
2017-04-14 19:52:16 +02:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2018-03-14 21:53:35 +01:00
|
|
|
bucketName := mux.Vars(r)["bucketName"]
|
2017-04-14 19:52:16 +02:00
|
|
|
err := r.ParseMultipartForm(32 << 20)
|
|
|
|
if err != nil {
|
2017-05-25 18:33:44 +02:00
|
|
|
handleHTTPError(w, errors.Wrap(err, errParsingForm))
|
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
|
|
|
|
}
|
2017-04-19 14:18:58 +02:00
|
|
|
defer func() {
|
|
|
|
err = file.Close()
|
|
|
|
if err != nil {
|
2017-05-25 18:33:44 +02:00
|
|
|
log.Fatalln(errors.Wrap(err, "error closing file"))
|
2017-04-19 14:18:58 +02:00
|
|
|
}
|
|
|
|
}()
|
2017-03-30 22:48:27 +02:00
|
|
|
|
2017-12-05 23:30:28 +01:00
|
|
|
_, err = s3.PutObject(bucketName, handler.Filename, file, 1, minio.PutObjectOptions{ContentType: contentTypeOctetStream})
|
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)
|
2017-03-30 22:48:27 +02:00
|
|
|
})
|
|
|
|
}
|