From ac760626b3721682e8b8d4833ec2300a414387b1 Mon Sep 17 00:00:00 2001 From: Lena Fuhrimann <6780471+cloudlena@users.noreply.github.com> Date: Fri, 21 Apr 2017 10:36:22 +0200 Subject: [PATCH] Separate create object and copy object --- copy-object.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ create-object.go | 45 ++------------------------------------------- glide.lock | 4 ++-- main.go | 4 ++-- 4 files changed, 53 insertions(+), 47 deletions(-) create mode 100644 copy-object.go diff --git a/copy-object.go b/copy-object.go new file mode 100644 index 0000000..7ed3ad2 --- /dev/null +++ b/copy-object.go @@ -0,0 +1,47 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http" + + 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"` +} + +// CopyObjectHandler allows to copy an existing object +func CopyObjectHandler(s3 S3Client) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var copy CopyObjectInfo + + err := json.NewDecoder(r.Body).Decode(©) + 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 + } + }) +} diff --git a/create-object.go b/create-object.go index 3fb89ae..b182558 100644 --- a/create-object.go +++ b/create-object.go @@ -1,55 +1,14 @@ package main import ( - "encoding/json" - "fmt" "log" "net/http" "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"` -} - -// CreateObjectFromJSONHandler allows to copy an existing object -func CreateObjectFromJSONHandler(s3 S3Client) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - var copy CopyObjectInfo - - err := json.NewDecoder(r.Body).Decode(©) - 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 - } - }) -} - -// CreateObjectFromFormHandler allows to upload a new object -func CreateObjectFromFormHandler(s3 S3Client) http.Handler { +// 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) diff --git a/glide.lock b/glide.lock index e44071a..3a5ef56 100644 --- a/glide.lock +++ b/glide.lock @@ -1,12 +1,12 @@ hash: 9b88c8ce183463a407bdb87f79b6b857d8343a7bb5ce4723223c539c654d6d6d -updated: 2017-04-14T19:23:49.562207386+02:00 +updated: 2017-04-21T10:35:42.790104635+02:00 imports: - name: github.com/gorilla/context version: 1ea25387ff6f684839d82767c1733ff4d4d15d0a - name: github.com/gorilla/mux version: 392c28fe23e1c45ddba891b0320b3b5df220beea - name: github.com/mastertinner/adapters - version: 402cb2e8d9ff63dad093f386fc85ef2a36c42bfc + version: 5510790dd79e084a0b849ec99df4403af9344b45 subpackages: - logging - name: github.com/minio/minio-go diff --git a/main.go b/main.go index 83751e2..5c997bb 100644 --- a/main.go +++ b/main.go @@ -72,7 +72,7 @@ func main() { Headers(headerContentType, contentTypeJSON). Path("/{bucketName}/objects"). Handler(adapters.Adapt( - CreateObjectFromJSONHandler(s3), + CopyObjectHandler(s3), logging.Handler(logger), )) br. @@ -80,7 +80,7 @@ func main() { HeadersRegexp(headerContentType, contentTypeMultipartForm). Path("/{bucketName}/objects"). Handler(adapters.Adapt( - CreateObjectFromFormHandler(s3), + CreateObjectHandler(s3), logging.Handler(logger), )) br.