s3api_object_copy_handlers.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package s3api
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. )
  13. func (s3a *S3ApiServer) CopyObjectHandler(w http.ResponseWriter, r *http.Request) {
  14. dstBucket, dstObject := getBucketAndObject(r)
  15. // Copy source path.
  16. cpSrcPath, err := url.QueryUnescape(r.Header.Get("X-Amz-Copy-Source"))
  17. if err != nil {
  18. // Save unescaped string as is.
  19. cpSrcPath = r.Header.Get("X-Amz-Copy-Source")
  20. }
  21. srcBucket, srcObject := pathToBucketAndObject(cpSrcPath)
  22. // If source object is empty or bucket is empty, reply back invalid copy source.
  23. if srcObject == "" || srcBucket == "" {
  24. writeErrorResponse(w, s3err.ErrInvalidCopySource, r.URL)
  25. return
  26. }
  27. if srcBucket == dstBucket && srcObject == dstObject {
  28. writeErrorResponse(w, s3err.ErrInvalidCopySource, r.URL)
  29. return
  30. }
  31. dstUrl := fmt.Sprintf("http://%s%s/%s%s?collection=%s",
  32. s3a.option.Filer, s3a.option.BucketsPath, dstBucket, dstObject, dstBucket)
  33. srcUrl := fmt.Sprintf("http://%s%s/%s%s",
  34. s3a.option.Filer, s3a.option.BucketsPath, srcBucket, srcObject)
  35. _, _, resp, err := util.DownloadFile(srcUrl)
  36. if err != nil {
  37. writeErrorResponse(w, s3err.ErrInvalidCopySource, r.URL)
  38. return
  39. }
  40. defer util.CloseResponse(resp)
  41. glog.V(2).Infof("copy from %s to %s", srcUrl, dstUrl)
  42. etag, errCode := s3a.putToFiler(r, dstUrl, resp.Body)
  43. if errCode != s3err.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. dstBucket, _ := getBucketAndObject(r)
  70. // Copy source path.
  71. cpSrcPath, err := url.QueryUnescape(r.Header.Get("X-Amz-Copy-Source"))
  72. if err != nil {
  73. // Save unescaped string as is.
  74. cpSrcPath = r.Header.Get("X-Amz-Copy-Source")
  75. }
  76. srcBucket, srcObject := pathToBucketAndObject(cpSrcPath)
  77. // If source object is empty or bucket is empty, reply back invalid copy source.
  78. if srcObject == "" || srcBucket == "" {
  79. writeErrorResponse(w, s3err.ErrInvalidCopySource, r.URL)
  80. return
  81. }
  82. uploadID := r.URL.Query().Get("uploadId")
  83. partIDString := r.URL.Query().Get("partNumber")
  84. partID, err := strconv.Atoi(partIDString)
  85. if err != nil {
  86. writeErrorResponse(w, s3err.ErrInvalidPart, r.URL)
  87. return
  88. }
  89. // check partID with maximum part ID for multipart objects
  90. if partID > globalMaxPartID {
  91. writeErrorResponse(w, s3err.ErrInvalidMaxParts, r.URL)
  92. return
  93. }
  94. rangeHeader := r.Header.Get("x-amz-copy-source-range")
  95. dstUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s",
  96. s3a.option.Filer, s3a.genUploadsFolder(dstBucket), uploadID, partID, dstBucket)
  97. srcUrl := fmt.Sprintf("http://%s%s/%s%s",
  98. s3a.option.Filer, s3a.option.BucketsPath, srcBucket, srcObject)
  99. dataReader, err := util.ReadUrlAsReaderCloser(srcUrl, rangeHeader)
  100. if err != nil {
  101. writeErrorResponse(w, s3err.ErrInvalidCopySource, r.URL)
  102. return
  103. }
  104. defer dataReader.Close()
  105. glog.V(2).Infof("copy from %s to %s", srcUrl, dstUrl)
  106. etag, errCode := s3a.putToFiler(r, dstUrl, dataReader)
  107. if errCode != s3err.ErrNone {
  108. writeErrorResponse(w, errCode, r.URL)
  109. return
  110. }
  111. setEtag(w, etag)
  112. response := CopyPartResult{
  113. ETag: etag,
  114. LastModified: time.Now().UTC(),
  115. }
  116. writeSuccessResponseXML(w, encodeResponse(response))
  117. }