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.

188 lines
5.4 KiB

4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
5 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
  1. package s3api
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
  6. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  7. weed_server "github.com/chrislusf/seaweedfs/weed/server"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  14. )
  15. func (s3a *S3ApiServer) CopyObjectHandler(w http.ResponseWriter, r *http.Request) {
  16. dstBucket, dstObject := xhttp.GetBucketAndObject(r)
  17. // Copy source path.
  18. cpSrcPath, err := url.QueryUnescape(r.Header.Get("X-Amz-Copy-Source"))
  19. if err != nil {
  20. // Save unescaped string as is.
  21. cpSrcPath = r.Header.Get("X-Amz-Copy-Source")
  22. }
  23. srcBucket, srcObject := pathToBucketAndObject(cpSrcPath)
  24. glog.V(3).Infof("CopyObjectHandler %s %s => %s %s", srcBucket, srcObject, dstBucket, dstObject)
  25. if (srcBucket == dstBucket && srcObject == dstObject || cpSrcPath == "") && isReplace(r) {
  26. fullPath := util.FullPath(fmt.Sprintf("%s/%s%s", s3a.option.BucketsPath, dstBucket, dstObject))
  27. dir, name := fullPath.DirAndName()
  28. entry, err := s3a.getEntry(dir, name)
  29. if err != nil {
  30. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  31. return
  32. }
  33. entry.Extended = weed_server.SaveAmzMetaData(r, entry.Extended, isReplace(r))
  34. err = s3a.touch(dir, name, entry)
  35. if err != nil {
  36. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  37. return
  38. }
  39. writeSuccessResponseXML(w, r, CopyObjectResult{
  40. ETag: fmt.Sprintf("%x", entry.Attributes.Md5),
  41. LastModified: time.Now().UTC(),
  42. })
  43. return
  44. }
  45. // If source object is empty or bucket is empty, reply back invalid copy source.
  46. if srcObject == "" || srcBucket == "" {
  47. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  48. return
  49. }
  50. srcPath := util.FullPath(fmt.Sprintf("%s/%s%s", s3a.option.BucketsPath, srcBucket, srcObject))
  51. dir, name := srcPath.DirAndName()
  52. _, err = s3a.getEntry(dir, name)
  53. if err != nil {
  54. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  55. return
  56. }
  57. if srcBucket == dstBucket && srcObject == dstObject {
  58. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopyDest)
  59. return
  60. }
  61. dstUrl := fmt.Sprintf("http://%s%s/%s%s?collection=%s",
  62. s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, dstBucket, urlPathEscape(dstObject), dstBucket)
  63. srcUrl := fmt.Sprintf("http://%s%s/%s%s",
  64. s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, srcBucket, urlPathEscape(srcObject))
  65. _, _, resp, err := util.DownloadFile(srcUrl, "")
  66. if err != nil {
  67. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  68. return
  69. }
  70. defer util.CloseResponse(resp)
  71. glog.V(2).Infof("copy from %s to %s", srcUrl, dstUrl)
  72. etag, errCode := s3a.putToFiler(r, dstUrl, resp.Body)
  73. if errCode != s3err.ErrNone {
  74. s3err.WriteErrorResponse(w, r, errCode)
  75. return
  76. }
  77. setEtag(w, etag)
  78. response := CopyObjectResult{
  79. ETag: etag,
  80. LastModified: time.Now().UTC(),
  81. }
  82. writeSuccessResponseXML(w, r, response)
  83. }
  84. func pathToBucketAndObject(path string) (bucket, object string) {
  85. path = strings.TrimPrefix(path, "/")
  86. parts := strings.SplitN(path, "/", 2)
  87. if len(parts) == 2 {
  88. return parts[0], "/" + parts[1]
  89. }
  90. return parts[0], "/"
  91. }
  92. type CopyPartResult struct {
  93. LastModified time.Time `xml:"LastModified"`
  94. ETag string `xml:"ETag"`
  95. }
  96. func (s3a *S3ApiServer) CopyObjectPartHandler(w http.ResponseWriter, r *http.Request) {
  97. // https://docs.aws.amazon.com/AmazonS3/latest/dev/CopyingObjctsUsingRESTMPUapi.html
  98. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPartCopy.html
  99. dstBucket, _ := xhttp.GetBucketAndObject(r)
  100. // Copy source path.
  101. cpSrcPath, err := url.QueryUnescape(r.Header.Get("X-Amz-Copy-Source"))
  102. if err != nil {
  103. // Save unescaped string as is.
  104. cpSrcPath = r.Header.Get("X-Amz-Copy-Source")
  105. }
  106. srcBucket, srcObject := pathToBucketAndObject(cpSrcPath)
  107. // If source object is empty or bucket is empty, reply back invalid copy source.
  108. if srcObject == "" || srcBucket == "" {
  109. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  110. return
  111. }
  112. uploadID := r.URL.Query().Get("uploadId")
  113. partIDString := r.URL.Query().Get("partNumber")
  114. partID, err := strconv.Atoi(partIDString)
  115. if err != nil {
  116. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidPart)
  117. return
  118. }
  119. glog.V(3).Infof("CopyObjectPartHandler %s %s => %s part %d", srcBucket, srcObject, dstBucket, partID)
  120. // check partID with maximum part ID for multipart objects
  121. if partID > globalMaxPartID {
  122. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxParts)
  123. return
  124. }
  125. rangeHeader := r.Header.Get("x-amz-copy-source-range")
  126. dstUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s",
  127. s3a.option.Filer.ToHttpAddress(), s3a.genUploadsFolder(dstBucket), uploadID, partID, dstBucket)
  128. srcUrl := fmt.Sprintf("http://%s%s/%s%s",
  129. s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, srcBucket, urlPathEscape(srcObject))
  130. dataReader, err := util.ReadUrlAsReaderCloser(srcUrl, rangeHeader)
  131. if err != nil {
  132. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  133. return
  134. }
  135. defer dataReader.Close()
  136. glog.V(2).Infof("copy from %s to %s", srcUrl, dstUrl)
  137. etag, errCode := s3a.putToFiler(r, dstUrl, dataReader)
  138. if errCode != s3err.ErrNone {
  139. s3err.WriteErrorResponse(w, r, errCode)
  140. return
  141. }
  142. setEtag(w, etag)
  143. response := CopyPartResult{
  144. ETag: etag,
  145. LastModified: time.Now().UTC(),
  146. }
  147. writeSuccessResponseXML(w, r, response)
  148. }
  149. func isReplace(r *http.Request) bool {
  150. return r.Header.Get("X-Amz-Metadata-Directive") == "REPLACE"
  151. }