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.

266 lines
7.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. package s3api
  2. import (
  3. "encoding/xml"
  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. "io/ioutil"
  9. "net/http"
  10. "net/url"
  11. "sort"
  12. "strconv"
  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. // TODO handle encodingType
  122. writeSuccessResponseXML(w, encodeResponse(response))
  123. }
  124. // ListObjectPartsHandler - Lists object parts in a multipart upload.
  125. func (s3a *S3ApiServer) ListObjectPartsHandler(w http.ResponseWriter, r *http.Request) {
  126. vars := mux.Vars(r)
  127. bucket := vars["bucket"]
  128. object := vars["object"]
  129. uploadID, partNumberMarker, maxParts, _ := getObjectResources(r.URL.Query())
  130. if partNumberMarker < 0 {
  131. writeErrorResponse(w, ErrInvalidPartNumberMarker, r.URL)
  132. return
  133. }
  134. if maxParts < 0 {
  135. writeErrorResponse(w, ErrInvalidMaxParts, r.URL)
  136. return
  137. }
  138. response, errCode := s3a.listObjectParts(&s3.ListPartsInput{
  139. Bucket: aws.String(bucket),
  140. Key: aws.String(object),
  141. MaxParts: aws.Int64(int64(maxParts)),
  142. PartNumberMarker: aws.Int64(int64(partNumberMarker)),
  143. UploadId: aws.String(uploadID),
  144. })
  145. if errCode != ErrNone {
  146. writeErrorResponse(w, errCode, r.URL)
  147. return
  148. }
  149. writeSuccessResponseXML(w, encodeResponse(response))
  150. }
  151. // PutObjectPartHandler - Put an object part in a multipart upload.
  152. func (s3a *S3ApiServer) PutObjectPartHandler(w http.ResponseWriter, r *http.Request) {
  153. vars := mux.Vars(r)
  154. bucket := vars["bucket"]
  155. rAuthType := getRequestAuthType(r)
  156. uploadID := r.URL.Query().Get("uploadId")
  157. exists, err := s3a.exists(s3a.genUploadsFolder(bucket), uploadID, true)
  158. if !exists {
  159. writeErrorResponse(w, ErrNoSuchUpload, r.URL)
  160. return
  161. }
  162. partIDString := r.URL.Query().Get("partNumber")
  163. partID, err := strconv.Atoi(partIDString)
  164. if err != nil {
  165. writeErrorResponse(w, ErrInvalidPart, r.URL)
  166. return
  167. }
  168. if partID > globalMaxPartID {
  169. writeErrorResponse(w, ErrInvalidMaxParts, r.URL)
  170. return
  171. }
  172. dataReader := r.Body
  173. if rAuthType == authTypeStreamingSigned {
  174. dataReader = newSignV4ChunkedReader(r)
  175. }
  176. uploadUrl := fmt.Sprintf("http://%s%s/%s/%04d.part",
  177. s3a.option.Filer, s3a.genUploadsFolder(bucket), uploadID, partID-1)
  178. etag, errCode := s3a.putToFiler(r, uploadUrl, dataReader)
  179. if errCode != ErrNone {
  180. writeErrorResponse(w, errCode, r.URL)
  181. return
  182. }
  183. setEtag(w, etag)
  184. writeSuccessResponseEmpty(w)
  185. }
  186. func (s3a *S3ApiServer) genUploadsFolder(bucket string) string {
  187. return fmt.Sprintf("%s/%s/_uploads", s3a.option.BucketsPath, bucket)
  188. }
  189. // Parse bucket url queries for ?uploads
  190. func getBucketMultipartResources(values url.Values) (prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int, encodingType string) {
  191. prefix = values.Get("prefix")
  192. keyMarker = values.Get("key-marker")
  193. uploadIDMarker = values.Get("upload-id-marker")
  194. delimiter = values.Get("delimiter")
  195. if values.Get("max-uploads") != "" {
  196. maxUploads, _ = strconv.Atoi(values.Get("max-uploads"))
  197. } else {
  198. maxUploads = maxUploadsList
  199. }
  200. encodingType = values.Get("encoding-type")
  201. return
  202. }
  203. // Parse object url queries
  204. func getObjectResources(values url.Values) (uploadID string, partNumberMarker, maxParts int, encodingType string) {
  205. uploadID = values.Get("uploadId")
  206. partNumberMarker, _ = strconv.Atoi(values.Get("part-number-marker"))
  207. if values.Get("max-parts") != "" {
  208. maxParts, _ = strconv.Atoi(values.Get("max-parts"))
  209. } else {
  210. maxParts = maxPartsList
  211. }
  212. encodingType = values.Get("encoding-type")
  213. return
  214. }
  215. type byCompletedPartNumber []*s3.CompletedPart
  216. func (a byCompletedPartNumber) Len() int { return len(a) }
  217. func (a byCompletedPartNumber) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  218. func (a byCompletedPartNumber) Less(i, j int) bool { return *a[i].PartNumber < *a[j].PartNumber }