s3api_object_copy_handlers.go 4.8 KB

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