s3manager-web/create-object.go
2017-04-09 16:28:57 +02:00

74 lines
1.9 KiB
Go

package main
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/gorilla/mux"
minio "github.com/minio/minio-go"
)
// CopyObjectInfo is the information about an object to copy
type CopyObjectInfo struct {
BucketName string `json:"bucketName"`
ObjectName string `json:"objectName"`
SourceBucketName string `json:"sourceBucketName"`
SourceObjectName string `json:"sourceObjectName"`
}
// CreateObjectHandler allows to upload a new object
func CreateObjectHandler(s3 S3Client) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
if strings.Contains(r.Header.Get(headerContentType), contentTypeJSON) {
var copy CopyObjectInfo
err := json.NewDecoder(r.Body).Decode(&copy)
if err != nil {
handleHTTPError(w, http.StatusInternalServerError, err)
return
}
copyConds := minio.NewCopyConditions()
objectSource := fmt.Sprintf("/%s/%s", copy.SourceBucketName, copy.SourceObjectName)
err = s3.CopyObject(copy.BucketName, copy.ObjectName, objectSource, copyConds)
if err != nil {
handleHTTPError(w, http.StatusInternalServerError, err)
return
}
w.Header().Set(headerContentType, contentTypeJSON)
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(copy)
if err != nil {
handleHTTPError(w, http.StatusInternalServerError, err)
return
}
} else {
err := r.ParseMultipartForm(32 << 20)
if err != nil {
handleHTTPError(w, http.StatusUnprocessableEntity, err)
return
}
file, handler, err := r.FormFile("file")
if err != nil {
handleHTTPError(w, http.StatusInternalServerError, err)
return
}
defer file.Close()
_, err = s3.PutObject(vars["bucketName"], handler.Filename, file, contentTypeOctetStream)
if err != nil {
handleHTTPError(w, http.StatusInternalServerError, err)
return
}
w.WriteHeader(http.StatusCreated)
}
})
}