You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
7.3 KiB

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