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.

146 lines
3.8 KiB

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