Separate handlers for create object

This commit is contained in:
Lena Fuhrimann 2017-04-14 19:52:16 +02:00
parent abcdae86a9
commit 6778d23090
4 changed files with 64 additions and 52 deletions

View file

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings"
"github.com/gorilla/mux" "github.com/gorilla/mux"
minio "github.com/minio/minio-go" minio "github.com/minio/minio-go"
@ -18,12 +17,9 @@ type CopyObjectInfo struct {
SourceObjectName string `json:"sourceObjectName"` SourceObjectName string `json:"sourceObjectName"`
} }
// CreateObjectHandler allows to upload a new object // CreateObjectFromJSONHandler allows to copy an existing object
func CreateObjectHandler(s3 S3Client) http.Handler { func CreateObjectFromJSONHandler(s3 S3Client) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 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 var copy CopyObjectInfo
err := json.NewDecoder(r.Body).Decode(&copy) err := json.NewDecoder(r.Body).Decode(&copy)
@ -48,7 +44,14 @@ func CreateObjectHandler(s3 S3Client) http.Handler {
handleHTTPError(w, http.StatusInternalServerError, err) handleHTTPError(w, http.StatusInternalServerError, err)
return return
} }
} else { })
}
// CreateObjectFromFormHandler allows to upload a new object
func CreateObjectFromFormHandler(s3 S3Client) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
err := r.ParseMultipartForm(32 << 20) err := r.ParseMultipartForm(32 << 20)
if err != nil { if err != nil {
handleHTTPError(w, http.StatusUnprocessableEntity, err) handleHTTPError(w, http.StatusUnprocessableEntity, err)
@ -69,6 +72,5 @@ func CreateObjectHandler(s3 S3Client) http.Handler {
} }
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
}
}) })
} }

4
glide.lock generated
View file

@ -1,8 +1,8 @@
hash: 9b88c8ce183463a407bdb87f79b6b857d8343a7bb5ce4723223c539c654d6d6d hash: 9b88c8ce183463a407bdb87f79b6b857d8343a7bb5ce4723223c539c654d6d6d
updated: 2017-04-11T20:07:50.857342641+02:00 updated: 2017-04-14T19:23:49.562207386+02:00
imports: imports:
- name: github.com/gorilla/context - name: github.com/gorilla/context
version: 08b5f424b9271eedf6f9f0ce86cb9396ed337a42 version: 1ea25387ff6f684839d82767c1733ff4d4d15d0a
- name: github.com/gorilla/mux - name: github.com/gorilla/mux
version: 392c28fe23e1c45ddba891b0320b3b5df220beea version: 392c28fe23e1c45ddba891b0320b3b5df220beea
- name: github.com/mastertinner/adapters - name: github.com/mastertinner/adapters

12
main.go
View file

@ -15,6 +15,7 @@ const (
headerContentType = "Content-Type" headerContentType = "Content-Type"
headerContentDisposition = "Content-Disposition" headerContentDisposition = "Content-Disposition"
contentTypeJSON = "application/json" contentTypeJSON = "application/json"
contentTypeMultipartForm = "multipart/form-data"
contentTypeOctetStream = "application/octet-stream" contentTypeOctetStream = "application/octet-stream"
) )
@ -68,9 +69,18 @@ func main() {
)) ))
br. br.
Methods(http.MethodPost). Methods(http.MethodPost).
Headers(headerContentType, contentTypeJSON).
Path("/{bucketName}/objects"). Path("/{bucketName}/objects").
Handler(adapters.Adapt( Handler(adapters.Adapt(
CreateObjectHandler(s3), CreateObjectFromJSONHandler(s3),
logging.Handler(logger),
))
br.
Methods(http.MethodPost).
HeadersRegexp(headerContentType, contentTypeMultipartForm).
Path("/{bucketName}/objects").
Handler(adapters.Adapt(
CreateObjectFromFormHandler(s3),
logging.Handler(logger), logging.Handler(logger),
)) ))
br. br.

View file

@ -19,12 +19,12 @@ func newMinioClient() (*minio.Client, error) {
s3AccessKeyID := os.Getenv("S3_ACCESS_KEY_ID") s3AccessKeyID := os.Getenv("S3_ACCESS_KEY_ID")
if s3AccessKeyID == "" { if s3AccessKeyID == "" {
return c, errors.New("no S3_ACCESS_KEY_ID found") return nil, errors.New("no S3_ACCESS_KEY_ID found")
} }
s3SecretAccessKey := os.Getenv("S3_SECRET_ACCESS_KEY") s3SecretAccessKey := os.Getenv("S3_SECRET_ACCESS_KEY")
if s3SecretAccessKey == "" { if s3SecretAccessKey == "" {
return c, errors.New("no S3_SECRET_ACCESS_KEY found") return nil, errors.New("no S3_SECRET_ACCESS_KEY found")
} }
if os.Getenv("V2_SIGNING") == "true" { if os.Getenv("V2_SIGNING") == "true" {