Chris Lu
5 years ago
6 changed files with 225 additions and 6 deletions
-
14weed/s3api/s3api_errors.go
-
152weed/s3api/s3api_object_copy_handlers.go
-
3weed/s3api/s3api_object_handlers.go
-
9weed/s3api/s3api_server.go
-
32weed/s3api/s3api_test.go
-
21weed/util/http_util.go
@ -0,0 +1,152 @@ |
|||
package s3api |
|||
|
|||
import ( |
|||
"fmt" |
|||
"net/http" |
|||
"net/url" |
|||
"strconv" |
|||
"strings" |
|||
"time" |
|||
|
|||
"github.com/gorilla/mux" |
|||
|
|||
"github.com/chrislusf/seaweedfs/weed/util" |
|||
) |
|||
|
|||
func (s3a *S3ApiServer) CopyObjectHandler(w http.ResponseWriter, r *http.Request) { |
|||
|
|||
vars := mux.Vars(r) |
|||
dstBucket := vars["bucket"] |
|||
dstObject := getObject(vars) |
|||
|
|||
// 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 == "" { |
|||
writeErrorResponse(w, ErrInvalidCopySource, r.URL) |
|||
return |
|||
} |
|||
|
|||
if srcBucket == dstBucket && srcObject == dstObject { |
|||
writeErrorResponse(w, ErrInvalidCopySource, r.URL) |
|||
return |
|||
} |
|||
|
|||
dstUrl := fmt.Sprintf("http://%s%s/%s%s?collection=%s", |
|||
s3a.option.Filer, s3a.option.BucketsPath, dstBucket, dstObject, dstBucket) |
|||
srcUrl := fmt.Sprintf("http://%s%s/%s%s", |
|||
s3a.option.Filer, s3a.option.BucketsPath, srcBucket, srcObject) |
|||
|
|||
_, _, dataReader, err := util.DownloadFile(srcUrl) |
|||
if err != nil { |
|||
writeErrorResponse(w, ErrInvalidCopySource, r.URL) |
|||
return |
|||
} |
|||
|
|||
etag, errCode := s3a.putToFiler(r, dstUrl, dataReader) |
|||
|
|||
println("srcUrl:", srcUrl) |
|||
println("dstUrl:", dstUrl) |
|||
|
|||
if errCode != ErrNone { |
|||
writeErrorResponse(w, errCode, r.URL) |
|||
return |
|||
} |
|||
|
|||
setEtag(w, etag) |
|||
|
|||
response := CopyObjectResult{ |
|||
ETag: etag, |
|||
LastModified: time.Now(), |
|||
} |
|||
|
|||
writeSuccessResponseXML(w, encodeResponse(response)) |
|||
|
|||
} |
|||
|
|||
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
|
|||
vars := mux.Vars(r) |
|||
dstBucket := vars["bucket"] |
|||
// dstObject := getObject(vars)
|
|||
|
|||
// 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 == "" { |
|||
writeErrorResponse(w, ErrInvalidCopySource, r.URL) |
|||
return |
|||
} |
|||
|
|||
uploadID := r.URL.Query().Get("uploadId") |
|||
partIDString := r.URL.Query().Get("partNumber") |
|||
|
|||
partID, err := strconv.Atoi(partIDString) |
|||
if err != nil { |
|||
writeErrorResponse(w, ErrInvalidPart, r.URL) |
|||
return |
|||
} |
|||
|
|||
// check partID with maximum part ID for multipart objects
|
|||
if partID > 10000 { |
|||
writeErrorResponse(w, ErrInvalidMaxParts, r.URL) |
|||
return |
|||
} |
|||
|
|||
rangeHeader := r.Header.Get("x-amz-copy-source-range") |
|||
|
|||
dstUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s", |
|||
s3a.option.Filer, s3a.genUploadsFolder(dstBucket), uploadID, partID-1, dstBucket) |
|||
srcUrl := fmt.Sprintf("http://%s%s/%s%s", |
|||
s3a.option.Filer, s3a.option.BucketsPath, srcBucket, srcObject) |
|||
|
|||
dataReader, err := util.ReadUrlAsReaderCloser(srcUrl, rangeHeader) |
|||
if err != nil { |
|||
writeErrorResponse(w, ErrInvalidCopySource, r.URL) |
|||
return |
|||
} |
|||
|
|||
etag, errCode := s3a.putToFiler(r, dstUrl, dataReader) |
|||
|
|||
if errCode != ErrNone { |
|||
writeErrorResponse(w, errCode, r.URL) |
|||
return |
|||
} |
|||
|
|||
setEtag(w, etag) |
|||
|
|||
response := CopyPartResult{ |
|||
ETag: etag, |
|||
LastModified: time.Now(), |
|||
} |
|||
|
|||
writeSuccessResponseXML(w, encodeResponse(response)) |
|||
|
|||
} |
@ -0,0 +1,32 @@ |
|||
package s3api |
|||
|
|||
import ( |
|||
"testing" |
|||
"time" |
|||
) |
|||
|
|||
func TestCopyObjectResponse(t *testing.T) { |
|||
|
|||
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_CopyObject.html
|
|||
|
|||
response := CopyObjectResult{ |
|||
ETag: "12345678", |
|||
LastModified: time.Now(), |
|||
} |
|||
|
|||
println(string(encodeResponse(response))) |
|||
|
|||
} |
|||
|
|||
func TestCopyPartResponse(t *testing.T) { |
|||
|
|||
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPartCopy.html
|
|||
|
|||
response := CopyPartResult{ |
|||
ETag: "12345678", |
|||
LastModified: time.Now(), |
|||
} |
|||
|
|||
println(string(encodeResponse(response))) |
|||
|
|||
} |
Reference in new issue
xxxxxxxxxx