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.

152 lines
3.8 KiB

  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. etag, errCode := s3a.putToFiler(r, dstUrl, dataReader)
  42. println("srcUrl:", srcUrl)
  43. println("dstUrl:", dstUrl)
  44. if errCode != ErrNone {
  45. writeErrorResponse(w, errCode, r.URL)
  46. return
  47. }
  48. setEtag(w, etag)
  49. response := CopyObjectResult{
  50. ETag: etag,
  51. LastModified: time.Now(),
  52. }
  53. writeSuccessResponseXML(w, encodeResponse(response))
  54. }
  55. func pathToBucketAndObject(path string) (bucket, object string) {
  56. path = strings.TrimPrefix(path, "/")
  57. parts := strings.SplitN(path, "/", 2)
  58. if len(parts) == 2 {
  59. return parts[0], "/" + parts[1]
  60. }
  61. return parts[0], "/"
  62. }
  63. type CopyPartResult struct {
  64. LastModified time.Time `xml:"LastModified"`
  65. ETag string `xml:"ETag"`
  66. }
  67. func (s3a *S3ApiServer) CopyObjectPartHandler(w http.ResponseWriter, r *http.Request) {
  68. // https://docs.aws.amazon.com/AmazonS3/latest/dev/CopyingObjctsUsingRESTMPUapi.html
  69. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPartCopy.html
  70. vars := mux.Vars(r)
  71. dstBucket := vars["bucket"]
  72. // dstObject := getObject(vars)
  73. // Copy source path.
  74. cpSrcPath, err := url.QueryUnescape(r.Header.Get("X-Amz-Copy-Source"))
  75. if err != nil {
  76. // Save unescaped string as is.
  77. cpSrcPath = r.Header.Get("X-Amz-Copy-Source")
  78. }
  79. srcBucket, srcObject := pathToBucketAndObject(cpSrcPath)
  80. // If source object is empty or bucket is empty, reply back invalid copy source.
  81. if srcObject == "" || srcBucket == "" {
  82. writeErrorResponse(w, ErrInvalidCopySource, r.URL)
  83. return
  84. }
  85. uploadID := r.URL.Query().Get("uploadId")
  86. partIDString := r.URL.Query().Get("partNumber")
  87. partID, err := strconv.Atoi(partIDString)
  88. if err != nil {
  89. writeErrorResponse(w, ErrInvalidPart, r.URL)
  90. return
  91. }
  92. // check partID with maximum part ID for multipart objects
  93. if partID > 10000 {
  94. writeErrorResponse(w, ErrInvalidMaxParts, r.URL)
  95. return
  96. }
  97. rangeHeader := r.Header.Get("x-amz-copy-source-range")
  98. dstUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s",
  99. s3a.option.Filer, s3a.genUploadsFolder(dstBucket), uploadID, partID-1, dstBucket)
  100. srcUrl := fmt.Sprintf("http://%s%s/%s%s",
  101. s3a.option.Filer, s3a.option.BucketsPath, srcBucket, srcObject)
  102. dataReader, err := util.ReadUrlAsReaderCloser(srcUrl, rangeHeader)
  103. if err != nil {
  104. writeErrorResponse(w, ErrInvalidCopySource, r.URL)
  105. return
  106. }
  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(),
  116. }
  117. writeSuccessResponseXML(w, encodeResponse(response))
  118. }