s3manager-web/internal/app/s3manager/copy_object.go

51 lines
1.4 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"
"net/http"
2017-05-25 18:33:44 +02:00
"github.com/pkg/errors"
2017-04-21 10:36:22 +02:00
minio "github.com/minio/minio-go"
)
2017-05-08 23:07:07 +02:00
// CopyObjectHandler copies an existing object under a new name.
func CopyObjectHandler(s3 S3) http.Handler {
2018-05-22 22:56:01 +02:00
// request is the information about an object to copy.
type request struct {
BucketName string `json:"bucketName"`
ObjectName string `json:"objectName"`
SourceBucketName string `json:"sourceBucketName"`
SourceObjectName string `json:"sourceObjectName"`
}
2017-04-21 10:36:22 +02:00
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2018-05-22 22:56:01 +02:00
var req request
err := json.NewDecoder(r.Body).Decode(&req)
2017-04-21 10:36:22 +02:00
if err != nil {
2018-05-22 22:56:01 +02:00
handleHTTPError(w, errors.Wrap(err, "error decoding body JSON"))
2017-04-21 10:36:22 +02:00
return
}
2018-05-22 22:56:01 +02:00
src := minio.NewSourceInfo(req.SourceBucketName, req.SourceObjectName, nil)
dst, err := minio.NewDestinationInfo(req.BucketName, req.ObjectName, nil, nil)
2017-07-31 10:57:22 +02:00
if err != nil {
handleHTTPError(w, errors.Wrap(err, "error creating destination for copying"))
return
}
err = s3.CopyObject(dst, src)
2017-04-21 10:36:22 +02:00
if err != nil {
2017-05-25 18:33:44 +02:00
handleHTTPError(w, errors.Wrap(err, "error copying object"))
2017-04-21 10:36:22 +02:00
return
}
2018-05-22 22:56:01 +02:00
w.Header().Set("Content-Type", "application/json; charset=utf-8")
2017-04-21 10:36:22 +02:00
w.WriteHeader(http.StatusCreated)
2018-05-22 22:56:01 +02:00
err = json.NewEncoder(w).Encode(req)
2017-04-21 10:36:22 +02:00
if err != nil {
2018-05-22 22:56:01 +02:00
handleHTTPError(w, errors.Wrap(err, "error encoding JSON"))
2017-04-21 10:36:22 +02:00
return
}
})
}