s3api_object_multipart_handlers.go 7.8 KB

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