s3api_object_copy_handlers.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 || entry.IsDirectory {
  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. if entry, err := s3a.getEntry(dir, name); err != nil || entry.IsDirectory {
  53. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  54. return
  55. }
  56. if srcBucket == dstBucket && srcObject == dstObject {
  57. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopyDest)
  58. return
  59. }
  60. dstUrl := fmt.Sprintf("http://%s%s/%s%s?collection=%s",
  61. s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, dstBucket, urlPathEscape(dstObject), dstBucket)
  62. srcUrl := fmt.Sprintf("http://%s%s/%s%s",
  63. s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, srcBucket, urlPathEscape(srcObject))
  64. _, _, resp, err := util.DownloadFile(srcUrl, s3a.maybeGetFilerJwtAuthorizationToken(false))
  65. if err != nil {
  66. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  67. return
  68. }
  69. defer util.CloseResponse(resp)
  70. glog.V(2).Infof("copy from %s to %s", srcUrl, dstUrl)
  71. etag, errCode := s3a.putToFiler(r, dstUrl, resp.Body)
  72. if errCode != s3err.ErrNone {
  73. s3err.WriteErrorResponse(w, r, errCode)
  74. return
  75. }
  76. setEtag(w, etag)
  77. response := CopyObjectResult{
  78. ETag: etag,
  79. LastModified: time.Now().UTC(),
  80. }
  81. writeSuccessResponseXML(w, r, response)
  82. }
  83. func pathToBucketAndObject(path string) (bucket, object string) {
  84. path = strings.TrimPrefix(path, "/")
  85. parts := strings.SplitN(path, "/", 2)
  86. if len(parts) == 2 {
  87. return parts[0], "/" + parts[1]
  88. }
  89. return parts[0], "/"
  90. }
  91. type CopyPartResult struct {
  92. LastModified time.Time `xml:"LastModified"`
  93. ETag string `xml:"ETag"`
  94. }
  95. func (s3a *S3ApiServer) CopyObjectPartHandler(w http.ResponseWriter, r *http.Request) {
  96. // https://docs.aws.amazon.com/AmazonS3/latest/dev/CopyingObjctsUsingRESTMPUapi.html
  97. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_UploadPartCopy.html
  98. dstBucket, _ := xhttp.GetBucketAndObject(r)
  99. // Copy source path.
  100. cpSrcPath, err := url.QueryUnescape(r.Header.Get("X-Amz-Copy-Source"))
  101. if err != nil {
  102. // Save unescaped string as is.
  103. cpSrcPath = r.Header.Get("X-Amz-Copy-Source")
  104. }
  105. srcBucket, srcObject := pathToBucketAndObject(cpSrcPath)
  106. // If source object is empty or bucket is empty, reply back invalid copy source.
  107. if srcObject == "" || srcBucket == "" {
  108. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  109. return
  110. }
  111. uploadID := r.URL.Query().Get("uploadId")
  112. partIDString := r.URL.Query().Get("partNumber")
  113. partID, err := strconv.Atoi(partIDString)
  114. if err != nil {
  115. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidPart)
  116. return
  117. }
  118. glog.V(3).Infof("CopyObjectPartHandler %s %s => %s part %d", srcBucket, srcObject, dstBucket, partID)
  119. // check partID with maximum part ID for multipart objects
  120. if partID > globalMaxPartID {
  121. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxParts)
  122. return
  123. }
  124. rangeHeader := r.Header.Get("x-amz-copy-source-range")
  125. dstUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s",
  126. s3a.option.Filer.ToHttpAddress(), s3a.genUploadsFolder(dstBucket), uploadID, partID, dstBucket)
  127. srcUrl := fmt.Sprintf("http://%s%s/%s%s",
  128. s3a.option.Filer.ToHttpAddress(), s3a.option.BucketsPath, srcBucket, urlPathEscape(srcObject))
  129. dataReader, err := util.ReadUrlAsReaderCloser(srcUrl, s3a.maybeGetFilerJwtAuthorizationToken(false), rangeHeader)
  130. if err != nil {
  131. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidCopySource)
  132. return
  133. }
  134. defer dataReader.Close()
  135. glog.V(2).Infof("copy from %s to %s", srcUrl, dstUrl)
  136. etag, errCode := s3a.putToFiler(r, dstUrl, dataReader)
  137. if errCode != s3err.ErrNone {
  138. s3err.WriteErrorResponse(w, r, errCode)
  139. return
  140. }
  141. setEtag(w, etag)
  142. response := CopyPartResult{
  143. ETag: etag,
  144. LastModified: time.Now().UTC(),
  145. }
  146. writeSuccessResponseXML(w, r, response)
  147. }
  148. func isReplace(r *http.Request) bool {
  149. return r.Header.Get("X-Amz-Metadata-Directive") == "REPLACE"
  150. }