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.

255 lines
7.2 KiB

6 years ago
  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: 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: 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: 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: 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. uploadUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s",
  162. s3a.option.Filer, s3a.genUploadsFolder(bucket), uploadID, partID-1, bucket)
  163. etag, errCode := s3a.putToFiler(r, uploadUrl, dataReader)
  164. if errCode != ErrNone {
  165. writeErrorResponse(w, errCode, r.URL)
  166. return
  167. }
  168. setEtag(w, etag)
  169. writeSuccessResponseEmpty(w)
  170. }
  171. func (s3a *S3ApiServer) genUploadsFolder(bucket string) string {
  172. return fmt.Sprintf("%s/%s/.uploads", s3a.option.BucketsPath, bucket)
  173. }
  174. // Parse bucket url queries for ?uploads
  175. func getBucketMultipartResources(values url.Values) (prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int, encodingType string) {
  176. prefix = values.Get("prefix")
  177. keyMarker = values.Get("key-marker")
  178. uploadIDMarker = values.Get("upload-id-marker")
  179. delimiter = values.Get("delimiter")
  180. if values.Get("max-uploads") != "" {
  181. maxUploads, _ = strconv.Atoi(values.Get("max-uploads"))
  182. } else {
  183. maxUploads = maxUploadsList
  184. }
  185. encodingType = values.Get("encoding-type")
  186. return
  187. }
  188. // Parse object url queries
  189. func getObjectResources(values url.Values) (uploadID string, partNumberMarker, maxParts int, encodingType string) {
  190. uploadID = values.Get("uploadId")
  191. partNumberMarker, _ = strconv.Atoi(values.Get("part-number-marker"))
  192. if values.Get("max-parts") != "" {
  193. maxParts, _ = strconv.Atoi(values.Get("max-parts"))
  194. } else {
  195. maxParts = maxPartsList
  196. }
  197. encodingType = values.Get("encoding-type")
  198. return
  199. }
  200. type byCompletedPartNumber []*s3.CompletedPart
  201. func (a byCompletedPartNumber) Len() int { return len(a) }
  202. func (a byCompletedPartNumber) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  203. func (a byCompletedPartNumber) Less(i, j int) bool { return *a[i].PartNumber < *a[j].PartNumber }