s3manager-web/copy_object.go

48 lines
1.2 KiB
Go
Raw Normal View History

2017-05-08 23:07:07 +02:00
package s3manager
2017-04-21 10:36:22 +02:00
import (
"encoding/json"
"fmt"
"net/http"
minio "github.com/minio/minio-go"
)
2017-05-08 23:07:07 +02:00
// copyObjectInfo is the information about an object to copy.
type copyObjectInfo struct {
2017-04-21 10:36:22 +02:00
BucketName string `json:"bucketName"`
ObjectName string `json:"objectName"`
SourceBucketName string `json:"sourceBucketName"`
SourceObjectName string `json:"sourceObjectName"`
}
2017-05-08 23:07:07 +02:00
// CopyObjectHandler copies an existing object under a new name.
func CopyObjectHandler(s3 S3) http.Handler {
2017-04-21 10:36:22 +02:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2017-05-08 23:07:07 +02:00
var copy copyObjectInfo
2017-04-21 10:36:22 +02:00
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
}
2017-05-08 23:07:07 +02:00
w.Header().Set(HeaderContentType, ContentTypeJSON)
2017-04-21 10:36:22 +02:00
w.WriteHeader(http.StatusCreated)
err = json.NewEncoder(w).Encode(copy)
if err != nil {
handleHTTPError(w, http.StatusInternalServerError, err)
return
}
})
}