You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
3.8 KiB

5 years ago
  1. package s3api
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/gorilla/mux"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. func (s3a *S3ApiServer) CopyObjectHandler(w http.ResponseWriter, r *http.Request) {
  13. vars := mux.Vars(r)
  14. dstBucket := vars["bucket"]
  15. dstObject := getObject(vars)
  16. // Copy source path.
  17. cpSrcPath, err := url.QueryUnescape(r.Header.Get("X-Amz-Copy-Source"))
  18. if err != nil {
  19. // Save unescaped string as is.
  20. cpSrcPath = r.Header.Get("X-Amz-Copy-Source")
  21. }
  22. srcBucket, srcObject := pathToBucketAndObject(cpSrcPath)
  23. // If source object is empty or bucket is empty, reply back invalid copy source.
  24. if srcObject == "" || srcBucket == "" {
  25. writeErrorResponse(w, ErrInvalidCopySource, r.URL)
  26. return
  27. }
  28. if srcBucket == dstBucket && srcObject == dstObject {
  29. writeErrorResponse(w, ErrInvalidCopySource, r.URL)
  30. return
  31. }
  32. dstUrl := fmt.Sprintf("http://%s%s/%s%s?collection=%s",
  33. s3a.option.Filer, s3a.option.BucketsPath, dstBucket, dstObject, dstBucket)
  34. srcUrl := fmt.Sprintf("http://%s%s/%s%s",
  35. s3a.option.Filer, s3a.option.BucketsPath, srcBucket, srcObject)
  36. _, _, dataReader, err := util.DownloadFile(srcUrl)
  37. if err != nil {
  38. writeErrorResponse(w, ErrInvalidCopySource, r.URL)
  39. return
  40. }
  41. defer dataReader.Close()
  42. etag, errCode := s3a.putToFiler(r, dstUrl, dataReader)
  43. if errCode != ErrNone {
  44. writeErrorResponse(w, errCode, r.URL)
  45. return
  46. }
  47. setEtag(w, etag)
  48. response := CopyObjectResult{
  49. ETag: etag,
  50. LastModified: time.Now().UTC(),
  51. }
  52. writeSuccessResponseXML(w, encodeResponse(response))
  53. }
  54. func pathToBucketAndObject(path string) (bucket, object string) {
  55. path = strings.TrimPrefix(path, "/")
  56. parts := strings.SplitN(path, "/", 2)
  57. if len(parts) == 2 {
  58. return parts[0], "/" + parts[1]
  59. }
  60. return parts[0], "/"
  61. }
  62. type CopyPartResult struct {
  63. LastModified time.Time `xml:"LastModified"`
  64. ETag string `xml:"ETag"`
  65. }
  66. func (s3a *S3ApiServer) CopyObjectPartHandler(w http.ResponseWriter, r *http.Request) {
  67. // https://docs.aws.amazon.com/AmazonS3/latest/dev/CopyingObjctsUsingRESTMPUapi.html
  68. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPartCopy.html
  69. vars := mux.Vars(r)
  70. dstBucket := vars["bucket"]
  71. // dstObject := getObject(vars)
  72. // Copy source path.
  73. cpSrcPath, err := url.QueryUnescape(r.Header.Get("X-Amz-Copy-Source"))
  74. if err != nil {
  75. // Save unescaped string as is.
  76. cpSrcPath = r.Header.Get("X-Amz-Copy-Source")
  77. }
  78. srcBucket, srcObject := pathToBucketAndObject(cpSrcPath)
  79. // If source object is empty or bucket is empty, reply back invalid copy source.
  80. if srcObject == "" || srcBucket == "" {
  81. writeErrorResponse(w, ErrInvalidCopySource, r.URL)
  82. return
  83. }
  84. uploadID := r.URL.Query().Get("uploadId")
  85. partIDString := r.URL.Query().Get("partNumber")
  86. partID, err := strconv.Atoi(partIDString)
  87. if err != nil {
  88. writeErrorResponse(w, ErrInvalidPart, r.URL)
  89. return
  90. }
  91. // check partID with maximum part ID for multipart objects
  92. if partID > globalMaxPartID {
  93. writeErrorResponse(w, ErrInvalidMaxParts, r.URL)
  94. return
  95. }
  96. rangeHeader := r.Header.Get("x-amz-copy-source-range")
  97. dstUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s",
  98. s3a.option.Filer, s3a.genUploadsFolder(dstBucket), uploadID, partID-1, dstBucket)
  99. srcUrl := fmt.Sprintf("http://%s%s/%s%s",
  100. s3a.option.Filer, s3a.option.BucketsPath, srcBucket, srcObject)
  101. dataReader, err := util.ReadUrlAsReaderCloser(srcUrl, rangeHeader)
  102. if err != nil {
  103. writeErrorResponse(w, ErrInvalidCopySource, r.URL)
  104. return
  105. }
  106. defer dataReader.Close()
  107. etag, errCode := s3a.putToFiler(r, dstUrl, dataReader)
  108. if errCode != ErrNone {
  109. writeErrorResponse(w, errCode, r.URL)
  110. return
  111. }
  112. setEtag(w, etag)
  113. response := CopyPartResult{
  114. ETag: etag,
  115. LastModified: time.Now().UTC(),
  116. }
  117. writeSuccessResponseXML(w, encodeResponse(response))
  118. }