2020-01-31 08:11:08 +00:00
|
|
|
package s3api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-11-11 10:01:24 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2021-12-07 07:15:48 +00:00
|
|
|
xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
|
2020-09-19 21:09:58 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/s3api/s3err"
|
2021-03-19 08:31:49 +00:00
|
|
|
weed_server "github.com/chrislusf/seaweedfs/weed/server"
|
2020-01-31 08:11:08 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s3a *S3ApiServer) CopyObjectHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2021-12-07 07:15:48 +00:00
|
|
|
dstBucket, dstObject := xhttp.GetBucketAndObject(r)
|
2020-01-31 08:11:08 +00:00
|
|
|
|
|
|
|
// Copy source path.
|
|
|
|
cpSrcPath, err := url.QueryUnescape(r.Header.Get("X-Amz-Copy-Source"))
|
|
|
|
if err != nil {
|
|
|
|
// Save unescaped string as is.
|
|
|
|
cpSrcPath = r.Header.Get("X-Amz-Copy-Source")
|
|
|
|
}
|
|
|
|
|
|
|
|
srcBucket, srcObject := pathToBucketAndObject(cpSrcPath)
|
2021-03-19 08:31:49 +00:00
|
|
|
|
2021-09-19 07:18:59 +00:00
|
|
|
glog.V(3).Infof("CopyObjectHandler %s %s => %s %s", srcBucket, srcObject, dstBucket, dstObject)
|
|
|
|
|
2021-03-19 08:31:49 +00:00
|
|
|
if (srcBucket == dstBucket && srcObject == dstObject || cpSrcPath == "") && isReplace(r) {
|
|
|
|
fullPath := util.FullPath(fmt.Sprintf("%s/%s%s", s3a.option.BucketsPath, dstBucket, dstObject))
|
|
|
|
dir, name := fullPath.DirAndName()
|
|
|
|
entry, err := s3a.getEntry(dir, name)
|
|
|
|
if err != nil {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
|
2021-09-04 00:38:39 +00:00
|
|
|
return
|
2021-03-19 08:31:49 +00:00
|
|
|
}
|
|
|
|
entry.Extended = weed_server.SaveAmzMetaData(r, entry.Extended, isReplace(r))
|
|
|
|
err = s3a.touch(dir, name, entry)
|
|
|
|
if err != nil {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
|
2021-09-04 00:38:39 +00:00
|
|
|
return
|
2021-03-19 08:31:49 +00:00
|
|
|
}
|
2021-11-01 01:02:08 +00:00
|
|
|
writeSuccessResponseXML(w, r, CopyObjectResult{
|
2021-03-19 08:31:49 +00:00
|
|
|
ETag: fmt.Sprintf("%x", entry.Attributes.Md5),
|
|
|
|
LastModified: time.Now().UTC(),
|
2021-06-11 04:50:21 +00:00
|
|
|
})
|
2021-03-19 08:31:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-31 08:11:08 +00:00
|
|
|
// If source object is empty or bucket is empty, reply back invalid copy source.
|
|
|
|
if srcObject == "" || srcBucket == "" {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
|
2020-01-31 08:11:08 +00:00
|
|
|
return
|
|
|
|
}
|
2021-09-06 06:37:40 +00:00
|
|
|
srcPath := util.FullPath(fmt.Sprintf("%s/%s%s", s3a.option.BucketsPath, srcBucket, srcObject))
|
2021-09-04 00:38:39 +00:00
|
|
|
dir, name := srcPath.DirAndName()
|
|
|
|
_, err = s3a.getEntry(dir, name)
|
|
|
|
if err != nil {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
|
2021-09-04 00:38:39 +00:00
|
|
|
return
|
|
|
|
}
|
2020-01-31 08:11:08 +00:00
|
|
|
|
|
|
|
if srcBucket == dstBucket && srcObject == dstObject {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopyDest)
|
2020-01-31 08:11:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dstUrl := fmt.Sprintf("http://%s%s/%s%s?collection=%s",
|
2021-12-05 07:24:53 +00:00
|
|
|
s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, dstBucket, urlPathEscape(dstObject), dstBucket)
|
2020-01-31 08:11:08 +00:00
|
|
|
srcUrl := fmt.Sprintf("http://%s%s/%s%s",
|
2021-12-05 07:24:53 +00:00
|
|
|
s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, srcBucket, urlPathEscape(srcObject))
|
2020-01-31 08:11:08 +00:00
|
|
|
|
2021-08-13 04:40:33 +00:00
|
|
|
_, _, resp, err := util.DownloadFile(srcUrl, "")
|
2020-01-31 08:11:08 +00:00
|
|
|
if err != nil {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
|
2020-01-31 08:11:08 +00:00
|
|
|
return
|
|
|
|
}
|
2020-09-09 10:53:09 +00:00
|
|
|
defer util.CloseResponse(resp)
|
2020-01-31 08:11:08 +00:00
|
|
|
|
2020-11-11 10:01:24 +00:00
|
|
|
glog.V(2).Infof("copy from %s to %s", srcUrl, dstUrl)
|
2020-09-09 10:53:09 +00:00
|
|
|
etag, errCode := s3a.putToFiler(r, dstUrl, resp.Body)
|
2020-01-31 08:11:08 +00:00
|
|
|
|
2020-09-19 21:09:58 +00:00
|
|
|
if errCode != s3err.ErrNone {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, errCode)
|
2020-01-31 08:11:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setEtag(w, etag)
|
|
|
|
|
|
|
|
response := CopyObjectResult{
|
|
|
|
ETag: etag,
|
2020-07-25 07:52:31 +00:00
|
|
|
LastModified: time.Now().UTC(),
|
2020-01-31 08:11:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 01:02:08 +00:00
|
|
|
writeSuccessResponseXML(w, r, response)
|
2020-01-31 08:11:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func pathToBucketAndObject(path string) (bucket, object string) {
|
|
|
|
path = strings.TrimPrefix(path, "/")
|
|
|
|
parts := strings.SplitN(path, "/", 2)
|
|
|
|
if len(parts) == 2 {
|
|
|
|
return parts[0], "/" + parts[1]
|
|
|
|
}
|
|
|
|
return parts[0], "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
type CopyPartResult struct {
|
|
|
|
LastModified time.Time `xml:"LastModified"`
|
|
|
|
ETag string `xml:"ETag"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s3a *S3ApiServer) CopyObjectPartHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/dev/CopyingObjctsUsingRESTMPUapi.html
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPartCopy.html
|
2021-12-07 07:15:48 +00:00
|
|
|
dstBucket, _ := xhttp.GetBucketAndObject(r)
|
2020-01-31 08:11:08 +00:00
|
|
|
|
|
|
|
// Copy source path.
|
|
|
|
cpSrcPath, err := url.QueryUnescape(r.Header.Get("X-Amz-Copy-Source"))
|
|
|
|
if err != nil {
|
|
|
|
// Save unescaped string as is.
|
|
|
|
cpSrcPath = r.Header.Get("X-Amz-Copy-Source")
|
|
|
|
}
|
|
|
|
|
|
|
|
srcBucket, srcObject := pathToBucketAndObject(cpSrcPath)
|
|
|
|
// If source object is empty or bucket is empty, reply back invalid copy source.
|
|
|
|
if srcObject == "" || srcBucket == "" {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
|
2020-01-31 08:11:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadID := r.URL.Query().Get("uploadId")
|
|
|
|
partIDString := r.URL.Query().Get("partNumber")
|
|
|
|
|
|
|
|
partID, err := strconv.Atoi(partIDString)
|
|
|
|
if err != nil {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidPart)
|
2020-01-31 08:11:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-19 07:18:59 +00:00
|
|
|
glog.V(3).Infof("CopyObjectPartHandler %s %s => %s part %d", srcBucket, srcObject, dstBucket, partID)
|
|
|
|
|
2020-01-31 08:11:08 +00:00
|
|
|
// check partID with maximum part ID for multipart objects
|
2020-02-25 20:58:45 +00:00
|
|
|
if partID > globalMaxPartID {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxParts)
|
2020-01-31 08:11:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rangeHeader := r.Header.Get("x-amz-copy-source-range")
|
|
|
|
|
|
|
|
dstUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s",
|
2021-09-13 05:47:52 +00:00
|
|
|
s3a.option.Filer.ToHttpAddress(), s3a.genUploadsFolder(dstBucket), uploadID, partID, dstBucket)
|
2020-01-31 08:11:08 +00:00
|
|
|
srcUrl := fmt.Sprintf("http://%s%s/%s%s",
|
2021-12-05 07:24:53 +00:00
|
|
|
s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, srcBucket, urlPathEscape(srcObject))
|
2020-01-31 08:11:08 +00:00
|
|
|
|
|
|
|
dataReader, err := util.ReadUrlAsReaderCloser(srcUrl, rangeHeader)
|
|
|
|
if err != nil {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
|
2020-01-31 08:11:08 +00:00
|
|
|
return
|
|
|
|
}
|
2020-02-14 17:09:15 +00:00
|
|
|
defer dataReader.Close()
|
2020-01-31 08:11:08 +00:00
|
|
|
|
2020-11-11 10:01:24 +00:00
|
|
|
glog.V(2).Infof("copy from %s to %s", srcUrl, dstUrl)
|
2020-01-31 08:11:08 +00:00
|
|
|
etag, errCode := s3a.putToFiler(r, dstUrl, dataReader)
|
|
|
|
|
2020-09-19 21:09:58 +00:00
|
|
|
if errCode != s3err.ErrNone {
|
2021-11-01 01:05:34 +00:00
|
|
|
s3err.WriteErrorResponse(w, r, errCode)
|
2020-01-31 08:11:08 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
setEtag(w, etag)
|
|
|
|
|
|
|
|
response := CopyPartResult{
|
|
|
|
ETag: etag,
|
2020-07-25 07:52:31 +00:00
|
|
|
LastModified: time.Now().UTC(),
|
2020-01-31 08:11:08 +00:00
|
|
|
}
|
|
|
|
|
2021-11-01 01:02:08 +00:00
|
|
|
writeSuccessResponseXML(w, r, response)
|
2020-01-31 08:11:08 +00:00
|
|
|
|
|
|
|
}
|
2021-03-19 08:31:49 +00:00
|
|
|
|
|
|
|
func isReplace(r *http.Request) bool {
|
|
|
|
return r.Header.Get("X-Amz-Metadata-Directive") == "REPLACE"
|
|
|
|
}
|