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.

254 lines
7.4 KiB

4 years ago
6 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. package s3api
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "strings"
  9. "github.com/aws/aws-sdk-go/aws"
  10. "github.com/aws/aws-sdk-go/service/s3"
  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. bucket, object := getBucketAndObject(r)
  21. response, errCode := s3a.createMultipartUpload(&s3.CreateMultipartUploadInput{
  22. Bucket: aws.String(bucket),
  23. Key: objectKey(aws.String(object)),
  24. })
  25. if errCode != s3err.ErrNone {
  26. writeErrorResponse(w, errCode, r.URL)
  27. return
  28. }
  29. // println("NewMultipartUploadHandler", string(encodeResponse(response)))
  30. writeSuccessResponseXML(w, encodeResponse(response))
  31. }
  32. // CompleteMultipartUploadHandler - Completes multipart upload.
  33. func (s3a *S3ApiServer) CompleteMultipartUploadHandler(w http.ResponseWriter, r *http.Request) {
  34. bucket, object := getBucketAndObject(r)
  35. // Get upload id.
  36. uploadID, _, _, _ := getObjectResources(r.URL.Query())
  37. response, errCode := s3a.completeMultipartUpload(&s3.CompleteMultipartUploadInput{
  38. Bucket: aws.String(bucket),
  39. Key: objectKey(aws.String(object)),
  40. UploadId: aws.String(uploadID),
  41. })
  42. // println("CompleteMultipartUploadHandler", string(encodeResponse(response)), errCode)
  43. if errCode != s3err.ErrNone {
  44. writeErrorResponse(w, errCode, r.URL)
  45. return
  46. }
  47. writeSuccessResponseXML(w, encodeResponse(response))
  48. }
  49. // AbortMultipartUploadHandler - Aborts multipart upload.
  50. func (s3a *S3ApiServer) AbortMultipartUploadHandler(w http.ResponseWriter, r *http.Request) {
  51. bucket, object := getBucketAndObject(r)
  52. // Get upload id.
  53. uploadID, _, _, _ := getObjectResources(r.URL.Query())
  54. response, errCode := s3a.abortMultipartUpload(&s3.AbortMultipartUploadInput{
  55. Bucket: aws.String(bucket),
  56. Key: objectKey(aws.String(object)),
  57. UploadId: aws.String(uploadID),
  58. })
  59. if errCode != s3err.ErrNone {
  60. writeErrorResponse(w, errCode, r.URL)
  61. return
  62. }
  63. // println("AbortMultipartUploadHandler", string(encodeResponse(response)))
  64. writeSuccessResponseXML(w, encodeResponse(response))
  65. }
  66. // ListMultipartUploadsHandler - Lists multipart uploads.
  67. func (s3a *S3ApiServer) ListMultipartUploadsHandler(w http.ResponseWriter, r *http.Request) {
  68. bucket, _ := getBucketAndObject(r)
  69. prefix, keyMarker, uploadIDMarker, delimiter, maxUploads, encodingType := getBucketMultipartResources(r.URL.Query())
  70. if maxUploads < 0 {
  71. writeErrorResponse(w, s3err.ErrInvalidMaxUploads, r.URL)
  72. return
  73. }
  74. if keyMarker != "" {
  75. // Marker not common with prefix is not implemented.
  76. if !strings.HasPrefix(keyMarker, prefix) {
  77. writeErrorResponse(w, s3err.ErrNotImplemented, r.URL)
  78. return
  79. }
  80. }
  81. response, errCode := s3a.listMultipartUploads(&s3.ListMultipartUploadsInput{
  82. Bucket: aws.String(bucket),
  83. Delimiter: aws.String(delimiter),
  84. EncodingType: aws.String(encodingType),
  85. KeyMarker: aws.String(keyMarker),
  86. MaxUploads: aws.Int64(int64(maxUploads)),
  87. Prefix: aws.String(prefix),
  88. UploadIdMarker: aws.String(uploadIDMarker),
  89. })
  90. if errCode != s3err.ErrNone {
  91. writeErrorResponse(w, errCode, r.URL)
  92. return
  93. }
  94. // TODO handle encodingType
  95. // println("ListMultipartUploadsHandler", string(encodeResponse(response)))
  96. writeSuccessResponseXML(w, encodeResponse(response))
  97. }
  98. // ListObjectPartsHandler - Lists object parts in a multipart upload.
  99. func (s3a *S3ApiServer) ListObjectPartsHandler(w http.ResponseWriter, r *http.Request) {
  100. bucket, object := getBucketAndObject(r)
  101. uploadID, partNumberMarker, maxParts, _ := getObjectResources(r.URL.Query())
  102. if partNumberMarker < 0 {
  103. writeErrorResponse(w, s3err.ErrInvalidPartNumberMarker, r.URL)
  104. return
  105. }
  106. if maxParts < 0 {
  107. writeErrorResponse(w, s3err.ErrInvalidMaxParts, r.URL)
  108. return
  109. }
  110. response, errCode := s3a.listObjectParts(&s3.ListPartsInput{
  111. Bucket: aws.String(bucket),
  112. Key: objectKey(aws.String(object)),
  113. MaxParts: aws.Int64(int64(maxParts)),
  114. PartNumberMarker: aws.Int64(int64(partNumberMarker)),
  115. UploadId: aws.String(uploadID),
  116. })
  117. if errCode != s3err.ErrNone {
  118. writeErrorResponse(w, errCode, r.URL)
  119. return
  120. }
  121. // println("ListObjectPartsHandler", string(encodeResponse(response)))
  122. writeSuccessResponseXML(w, encodeResponse(response))
  123. }
  124. // PutObjectPartHandler - Put an object part in a multipart upload.
  125. func (s3a *S3ApiServer) PutObjectPartHandler(w http.ResponseWriter, r *http.Request) {
  126. bucket, _ := getBucketAndObject(r)
  127. uploadID := r.URL.Query().Get("uploadId")
  128. exists, err := s3a.exists(s3a.genUploadsFolder(bucket), uploadID, true)
  129. if !exists {
  130. writeErrorResponse(w, s3err.ErrNoSuchUpload, r.URL)
  131. return
  132. }
  133. partIDString := r.URL.Query().Get("partNumber")
  134. partID, err := strconv.Atoi(partIDString)
  135. if err != nil {
  136. writeErrorResponse(w, s3err.ErrInvalidPart, r.URL)
  137. return
  138. }
  139. if partID > globalMaxPartID {
  140. writeErrorResponse(w, s3err.ErrInvalidMaxParts, r.URL)
  141. return
  142. }
  143. dataReader := r.Body
  144. if s3a.iam.isEnabled() {
  145. rAuthType := getRequestAuthType(r)
  146. var s3ErrCode s3err.ErrorCode
  147. switch rAuthType {
  148. case authTypeStreamingSigned:
  149. dataReader, s3ErrCode = s3a.iam.newSignV4ChunkedReader(r)
  150. case authTypeSignedV2, authTypePresignedV2:
  151. _, s3ErrCode = s3a.iam.isReqAuthenticatedV2(r)
  152. case authTypePresigned, authTypeSigned:
  153. _, s3ErrCode = s3a.iam.reqSignatureV4Verify(r)
  154. }
  155. if s3ErrCode != s3err.ErrNone {
  156. writeErrorResponse(w, s3ErrCode, r.URL)
  157. return
  158. }
  159. }
  160. defer dataReader.Close()
  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 != s3err.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 }