s3api_object_multipart_handlers.go 7.3 KB

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