s3api_object_copy_handlers.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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(),
  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(),
  116. }
  117. writeSuccessResponseXML(w, encodeResponse(response))
  118. }