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.

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