2020-08-19 23:35:51 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2021-05-18 11:10:08 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/layer"
|
2020-08-19 23:35:51 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-07-01 07:45:55 +00:00
|
|
|
type copyObjectArgs struct {
|
2021-08-06 15:08:09 +00:00
|
|
|
Conditional *conditionalArgs
|
|
|
|
MetadataDirective string
|
2021-07-01 07:45:55 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 15:08:09 +00:00
|
|
|
const replaceMetadataDirective = "REPLACE"
|
|
|
|
|
2020-08-19 23:35:51 +00:00
|
|
|
// path2BucketObject returns bucket and object.
|
|
|
|
func path2BucketObject(path string) (bucket, prefix string) {
|
|
|
|
path = strings.TrimPrefix(path, api.SlashSeparator)
|
|
|
|
m := strings.Index(path, api.SlashSeparator)
|
|
|
|
if m < 0 {
|
|
|
|
return path, ""
|
|
|
|
}
|
|
|
|
return path[:m], path[m+len(api.SlashSeparator):]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) CopyObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var (
|
2021-08-06 15:08:09 +00:00
|
|
|
err error
|
|
|
|
inf *layer.ObjectInfo
|
|
|
|
metadata map[string]string
|
2020-08-19 23:35:51 +00:00
|
|
|
|
2021-08-05 09:18:52 +00:00
|
|
|
reqInfo = api.GetReqInfo(r.Context())
|
2020-08-19 23:35:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
src := r.Header.Get("X-Amz-Copy-Source")
|
|
|
|
// Check https://docs.aws.amazon.com/AmazonS3/latest/dev/ObjectVersioning.html
|
|
|
|
// Regardless of whether you have enabled versioning, each object in your bucket
|
|
|
|
// has a version ID. If you have not enabled versioning, Amazon S3 sets the value
|
|
|
|
// of the version ID to null. If you have enabled versioning, Amazon S3 assigns a
|
|
|
|
// unique version ID value for the object.
|
|
|
|
if u, err := url.Parse(src); err == nil {
|
|
|
|
// Check if versionId query param was added, if yes then check if
|
|
|
|
// its non "null" value, we should error out since we do not support
|
|
|
|
// any versions other than "null".
|
|
|
|
if vid := u.Query().Get("versionId"); vid != "" && vid != "null" {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "no such version", reqInfo, api.GetAPIError(api.ErrNoSuchVersion))
|
2020-08-19 23:35:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
src = u.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
srcBucket, srcObject := path2BucketObject(src)
|
|
|
|
|
2021-07-01 07:45:55 +00:00
|
|
|
args, err := parseCopyObjectArgs(r.Header)
|
|
|
|
if err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not parse request params", reqInfo, err)
|
2021-07-01 07:45:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-06 15:08:09 +00:00
|
|
|
if args.MetadataDirective == replaceMetadataDirective {
|
|
|
|
metadata = parseMetadata(r)
|
|
|
|
} else if srcBucket == reqInfo.BucketName && srcObject == reqInfo.ObjectName {
|
|
|
|
h.logAndSendError(w, "could not copy to itself", reqInfo, api.GetAPIError(api.ErrInvalidRequest))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-01 07:45:55 +00:00
|
|
|
if inf, err = h.obj.GetObjectInfo(r.Context(), srcBucket, srcObject); err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "could not find object", reqInfo, err)
|
2021-07-01 07:45:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-14 12:51:45 +00:00
|
|
|
if err = checkPreconditions(inf, args.Conditional); err != nil {
|
2021-08-05 09:18:52 +00:00
|
|
|
h.logAndSendError(w, "precondition failed", reqInfo, api.GetAPIError(api.ErrPreconditionFailed))
|
2021-07-01 07:45:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-06 15:08:09 +00:00
|
|
|
if metadata == nil {
|
|
|
|
if len(inf.ContentType) > 0 {
|
|
|
|
inf.Headers[api.ContentType] = inf.ContentType
|
|
|
|
}
|
|
|
|
metadata = inf.Headers
|
|
|
|
} else if contentType := r.Header.Get(api.ContentType); len(contentType) > 0 {
|
|
|
|
metadata[api.ContentType] = contentType
|
|
|
|
}
|
|
|
|
|
2020-08-19 23:35:51 +00:00
|
|
|
params := &layer.CopyObjectParams{
|
|
|
|
SrcBucket: srcBucket,
|
2021-08-05 09:18:52 +00:00
|
|
|
DstBucket: reqInfo.BucketName,
|
2020-08-19 23:35:51 +00:00
|
|
|
SrcObject: srcObject,
|
2021-08-05 09:18:52 +00:00
|
|
|
DstObject: reqInfo.ObjectName,
|
2021-07-01 07:45:55 +00:00
|
|
|
SrcSize: inf.Size,
|
2021-08-06 15:08:09 +00:00
|
|
|
Header: metadata,
|
2020-08-19 23:35:51 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 15:08:09 +00:00
|
|
|
additional := []zap.Field{zap.String("src_bucket_name", srcBucket), zap.String("src_object_name", srcObject)}
|
2020-08-19 23:35:51 +00:00
|
|
|
if inf, err = h.obj.CopyObject(r.Context(), params); err != nil {
|
2021-08-06 15:08:09 +00:00
|
|
|
h.logAndSendError(w, "couldn't copy object", reqInfo, err, additional...)
|
2020-10-24 13:09:22 +00:00
|
|
|
return
|
2021-06-29 13:40:26 +00:00
|
|
|
} else if err = api.EncodeToResponse(w, &CopyObjectResponse{LastModified: inf.Created.Format(time.RFC3339), ETag: inf.HashSum}); err != nil {
|
2021-08-06 15:08:09 +00:00
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err, additional...)
|
2020-10-24 13:09:22 +00:00
|
|
|
return
|
2020-08-19 23:35:51 +00:00
|
|
|
}
|
2020-10-24 13:09:22 +00:00
|
|
|
|
|
|
|
h.log.Info("object is copied",
|
|
|
|
zap.String("bucket", inf.Bucket),
|
|
|
|
zap.String("object", inf.Name),
|
|
|
|
zap.Stringer("object_id", inf.ID()))
|
2020-08-19 23:35:51 +00:00
|
|
|
}
|
2021-07-01 07:45:55 +00:00
|
|
|
|
|
|
|
func parseCopyObjectArgs(headers http.Header) (*copyObjectArgs, error) {
|
|
|
|
var err error
|
2021-07-02 16:21:53 +00:00
|
|
|
args := &conditionalArgs{
|
|
|
|
IfMatch: headers.Get(api.AmzCopyIfMatch),
|
|
|
|
IfNoneMatch: headers.Get(api.AmzCopyIfNoneMatch),
|
|
|
|
}
|
2021-07-01 07:45:55 +00:00
|
|
|
|
|
|
|
if args.IfModifiedSince, err = parseHTTPTime(headers.Get(api.AmzCopyIfModifiedSince)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if args.IfUnmodifiedSince, err = parseHTTPTime(headers.Get(api.AmzCopyIfUnmodifiedSince)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-06 15:08:09 +00:00
|
|
|
copyArgs := ©ObjectArgs{Conditional: args}
|
|
|
|
copyArgs.MetadataDirective = headers.Get(api.AmzMetadataDirective)
|
|
|
|
|
|
|
|
return copyArgs, nil
|
2021-07-01 07:45:55 +00:00
|
|
|
}
|