s3api_object_multipart_handlers.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. "github.com/aws/aws-sdk-go/aws"
  13. "github.com/aws/aws-sdk-go/service/s3"
  14. )
  15. const (
  16. maxObjectListSizeLimit = 10000 // Limit number of objects in a listObjectsResponse.
  17. maxUploadsList = 10000 // Limit number of uploads in a listUploadsResponse.
  18. maxPartsList = 10000 // Limit number of parts in a listPartsResponse.
  19. globalMaxPartID = 100000
  20. )
  21. // NewMultipartUploadHandler - New multipart upload.
  22. func (s3a *S3ApiServer) NewMultipartUploadHandler(w http.ResponseWriter, r *http.Request) {
  23. bucket, object := xhttp.GetBucketAndObject(r)
  24. createMultipartUploadInput := &s3.CreateMultipartUploadInput{
  25. Bucket: aws.String(bucket),
  26. Key: objectKey(aws.String(object)),
  27. Metadata: make(map[string]*string),
  28. }
  29. metadata := weed_server.SaveAmzMetaData(r, nil, false)
  30. for k, v := range metadata {
  31. createMultipartUploadInput.Metadata[k] = aws.String(string(v))
  32. }
  33. contentType := r.Header.Get("Content-Type")
  34. if contentType != "" {
  35. createMultipartUploadInput.ContentType = &contentType
  36. }
  37. response, errCode := s3a.createMultipartUpload(createMultipartUploadInput)
  38. glog.V(2).Info("NewMultipartUploadHandler", string(s3err.EncodeXMLResponse(response)), errCode)
  39. if errCode != s3err.ErrNone {
  40. s3err.WriteErrorResponse(w, r, errCode)
  41. return
  42. }
  43. writeSuccessResponseXML(w, r, response)
  44. }
  45. // CompleteMultipartUploadHandler - Completes multipart upload.
  46. func (s3a *S3ApiServer) CompleteMultipartUploadHandler(w http.ResponseWriter, r *http.Request) {
  47. bucket, object := xhttp.GetBucketAndObject(r)
  48. // Get upload id.
  49. uploadID, _, _, _ := getObjectResources(r.URL.Query())
  50. response, errCode := s3a.completeMultipartUpload(&s3.CompleteMultipartUploadInput{
  51. Bucket: aws.String(bucket),
  52. Key: objectKey(aws.String(object)),
  53. UploadId: aws.String(uploadID),
  54. })
  55. glog.V(2).Info("CompleteMultipartUploadHandler", string(s3err.EncodeXMLResponse(response)), errCode)
  56. if errCode != s3err.ErrNone {
  57. s3err.WriteErrorResponse(w, r, errCode)
  58. return
  59. }
  60. writeSuccessResponseXML(w, r, response)
  61. }
  62. // AbortMultipartUploadHandler - Aborts multipart upload.
  63. func (s3a *S3ApiServer) AbortMultipartUploadHandler(w http.ResponseWriter, r *http.Request) {
  64. bucket, object := xhttp.GetBucketAndObject(r)
  65. // Get upload id.
  66. uploadID, _, _, _ := getObjectResources(r.URL.Query())
  67. response, errCode := s3a.abortMultipartUpload(&s3.AbortMultipartUploadInput{
  68. Bucket: aws.String(bucket),
  69. Key: objectKey(aws.String(object)),
  70. UploadId: aws.String(uploadID),
  71. })
  72. if errCode != s3err.ErrNone {
  73. s3err.WriteErrorResponse(w, r, errCode)
  74. return
  75. }
  76. glog.V(2).Info("AbortMultipartUploadHandler", string(s3err.EncodeXMLResponse(response)))
  77. writeSuccessResponseXML(w, r, response)
  78. }
  79. // ListMultipartUploadsHandler - Lists multipart uploads.
  80. func (s3a *S3ApiServer) ListMultipartUploadsHandler(w http.ResponseWriter, r *http.Request) {
  81. bucket, _ := xhttp.GetBucketAndObject(r)
  82. prefix, keyMarker, uploadIDMarker, delimiter, maxUploads, encodingType := getBucketMultipartResources(r.URL.Query())
  83. if maxUploads < 0 {
  84. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxUploads)
  85. return
  86. }
  87. if keyMarker != "" {
  88. // Marker not common with prefix is not implemented.
  89. if !strings.HasPrefix(keyMarker, prefix) {
  90. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  91. return
  92. }
  93. }
  94. response, errCode := s3a.listMultipartUploads(&s3.ListMultipartUploadsInput{
  95. Bucket: aws.String(bucket),
  96. Delimiter: aws.String(delimiter),
  97. EncodingType: aws.String(encodingType),
  98. KeyMarker: aws.String(keyMarker),
  99. MaxUploads: aws.Int64(int64(maxUploads)),
  100. Prefix: aws.String(prefix),
  101. UploadIdMarker: aws.String(uploadIDMarker),
  102. })
  103. glog.V(2).Infof("ListMultipartUploadsHandler %s errCode=%d", string(s3err.EncodeXMLResponse(response)), errCode)
  104. if errCode != s3err.ErrNone {
  105. s3err.WriteErrorResponse(w, r, errCode)
  106. return
  107. }
  108. // TODO handle encodingType
  109. writeSuccessResponseXML(w, r, response)
  110. }
  111. // ListObjectPartsHandler - Lists object parts in a multipart upload.
  112. func (s3a *S3ApiServer) ListObjectPartsHandler(w http.ResponseWriter, r *http.Request) {
  113. bucket, object := xhttp.GetBucketAndObject(r)
  114. uploadID, partNumberMarker, maxParts, _ := getObjectResources(r.URL.Query())
  115. if partNumberMarker < 0 {
  116. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidPartNumberMarker)
  117. return
  118. }
  119. if maxParts < 0 {
  120. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxParts)
  121. return
  122. }
  123. response, errCode := s3a.listObjectParts(&s3.ListPartsInput{
  124. Bucket: aws.String(bucket),
  125. Key: objectKey(aws.String(object)),
  126. MaxParts: aws.Int64(int64(maxParts)),
  127. PartNumberMarker: aws.Int64(int64(partNumberMarker)),
  128. UploadId: aws.String(uploadID),
  129. })
  130. if errCode != s3err.ErrNone {
  131. s3err.WriteErrorResponse(w, r, errCode)
  132. return
  133. }
  134. glog.V(2).Infof("ListObjectPartsHandler %s count=%d", string(s3err.EncodeXMLResponse(response)), len(response.Part))
  135. writeSuccessResponseXML(w, r, response)
  136. }
  137. // PutObjectPartHandler - Put an object part in a multipart upload.
  138. func (s3a *S3ApiServer) PutObjectPartHandler(w http.ResponseWriter, r *http.Request) {
  139. bucket, _ := xhttp.GetBucketAndObject(r)
  140. uploadID := r.URL.Query().Get("uploadId")
  141. exists, err := s3a.exists(s3a.genUploadsFolder(bucket), uploadID, true)
  142. if !exists {
  143. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchUpload)
  144. return
  145. }
  146. partIDString := r.URL.Query().Get("partNumber")
  147. partID, err := strconv.Atoi(partIDString)
  148. if err != nil {
  149. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidPart)
  150. return
  151. }
  152. if partID > globalMaxPartID {
  153. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxParts)
  154. return
  155. }
  156. dataReader := r.Body
  157. if s3a.iam.isEnabled() {
  158. rAuthType := getRequestAuthType(r)
  159. var s3ErrCode s3err.ErrorCode
  160. switch rAuthType {
  161. case authTypeStreamingSigned:
  162. dataReader, s3ErrCode = s3a.iam.newSignV4ChunkedReader(r)
  163. case authTypeSignedV2, authTypePresignedV2:
  164. _, s3ErrCode = s3a.iam.isReqAuthenticatedV2(r)
  165. case authTypePresigned, authTypeSigned:
  166. _, s3ErrCode = s3a.iam.reqSignatureV4Verify(r)
  167. }
  168. if s3ErrCode != s3err.ErrNone {
  169. s3err.WriteErrorResponse(w, r, s3ErrCode)
  170. return
  171. }
  172. }
  173. defer dataReader.Close()
  174. glog.V(2).Infof("PutObjectPartHandler %s %s %04d", bucket, uploadID, partID)
  175. uploadUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s",
  176. s3a.option.Filer.ToHttpAddress(), s3a.genUploadsFolder(bucket), uploadID, partID, bucket)
  177. if partID == 1 && r.Header.Get("Content-Type") == "" {
  178. dataReader = mimeDetect(r, dataReader)
  179. }
  180. etag, errCode := s3a.putToFiler(r, uploadUrl, dataReader)
  181. if errCode != s3err.ErrNone {
  182. s3err.WriteErrorResponse(w, r, errCode)
  183. return
  184. }
  185. setEtag(w, etag)
  186. writeSuccessResponseEmpty(w, r)
  187. }
  188. func (s3a *S3ApiServer) genUploadsFolder(bucket string) string {
  189. return fmt.Sprintf("%s/%s/.uploads", s3a.option.BucketsPath, bucket)
  190. }
  191. // Parse bucket url queries for ?uploads
  192. func getBucketMultipartResources(values url.Values) (prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int, encodingType string) {
  193. prefix = values.Get("prefix")
  194. keyMarker = values.Get("key-marker")
  195. uploadIDMarker = values.Get("upload-id-marker")
  196. delimiter = values.Get("delimiter")
  197. if values.Get("max-uploads") != "" {
  198. maxUploads, _ = strconv.Atoi(values.Get("max-uploads"))
  199. } else {
  200. maxUploads = maxUploadsList
  201. }
  202. encodingType = values.Get("encoding-type")
  203. return
  204. }
  205. // Parse object url queries
  206. func getObjectResources(values url.Values) (uploadID string, partNumberMarker, maxParts int, encodingType string) {
  207. uploadID = values.Get("uploadId")
  208. partNumberMarker, _ = strconv.Atoi(values.Get("part-number-marker"))
  209. if values.Get("max-parts") != "" {
  210. maxParts, _ = strconv.Atoi(values.Get("max-parts"))
  211. } else {
  212. maxParts = maxPartsList
  213. }
  214. encodingType = values.Get("encoding-type")
  215. return
  216. }
  217. type byCompletedPartNumber []*s3.CompletedPart
  218. func (a byCompletedPartNumber) Len() int { return len(a) }
  219. func (a byCompletedPartNumber) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  220. func (a byCompletedPartNumber) Less(i, j int) bool { return *a[i].PartNumber < *a[j].PartNumber }