s3api_object_multipart_handlers.go 7.3 KB

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