Separate create object and copy object

This commit is contained in:
Lena Fuhrimann 2017-04-21 10:36:22 +02:00
parent ae3afae893
commit ac760626b3
4 changed files with 53 additions and 47 deletions

47
copy-object.go Normal file
View file

@ -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(&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
}
})
}

View file

@ -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(&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
}
})
}
// 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)

4
glide.lock generated
View file

@ -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

View file

@ -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.