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.

301 lines
8.7 KiB

4 years ago
3 years ago
4 years ago
6 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
3 years ago
3 years ago
  1. package s3api
  2. import (
  3. "encoding/xml"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
  7. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  8. weed_server "github.com/chrislusf/seaweedfs/weed/server"
  9. "io"
  10. "net/http"
  11. "net/url"
  12. "strconv"
  13. "strings"
  14. "github.com/aws/aws-sdk-go/aws"
  15. "github.com/aws/aws-sdk-go/service/s3"
  16. )
  17. const (
  18. maxObjectListSizeLimit = 10000 // Limit number of objects in a listObjectsResponse.
  19. maxUploadsList = 10000 // Limit number of uploads in a listUploadsResponse.
  20. maxPartsList = 10000 // Limit number of parts in a listPartsResponse.
  21. globalMaxPartID = 100000
  22. )
  23. // NewMultipartUploadHandler - New multipart upload.
  24. func (s3a *S3ApiServer) NewMultipartUploadHandler(w http.ResponseWriter, r *http.Request) {
  25. bucket, object := xhttp.GetBucketAndObject(r)
  26. createMultipartUploadInput := &s3.CreateMultipartUploadInput{
  27. Bucket: aws.String(bucket),
  28. Key: objectKey(aws.String(object)),
  29. Metadata: make(map[string]*string),
  30. }
  31. metadata := weed_server.SaveAmzMetaData(r, nil, false)
  32. for k, v := range metadata {
  33. createMultipartUploadInput.Metadata[k] = aws.String(string(v))
  34. }
  35. contentType := r.Header.Get("Content-Type")
  36. if contentType != "" {
  37. createMultipartUploadInput.ContentType = &contentType
  38. }
  39. response, errCode := s3a.createMultipartUpload(createMultipartUploadInput)
  40. glog.V(2).Info("NewMultipartUploadHandler", string(s3err.EncodeXMLResponse(response)), errCode)
  41. if errCode != s3err.ErrNone {
  42. s3err.WriteErrorResponse(w, r, errCode)
  43. return
  44. }
  45. writeSuccessResponseXML(w, r, response)
  46. }
  47. // CompleteMultipartUploadHandler - Completes multipart upload.
  48. func (s3a *S3ApiServer) CompleteMultipartUploadHandler(w http.ResponseWriter, r *http.Request) {
  49. // https://docs.aws.amazon.com/AmazonS3/latest/API/API_CompleteMultipartUpload.html
  50. bucket, object := xhttp.GetBucketAndObject(r)
  51. parts := &CompleteMultipartUpload{}
  52. if err := xmlDecoder(r.Body, parts, r.ContentLength); err != nil {
  53. s3err.WriteErrorResponse(w, r, s3err.ErrMalformedXML)
  54. return
  55. }
  56. // Get upload id.
  57. uploadID, _, _, _ := getObjectResources(r.URL.Query())
  58. response, errCode := s3a.completeMultipartUpload(&s3.CompleteMultipartUploadInput{
  59. Bucket: aws.String(bucket),
  60. Key: objectKey(aws.String(object)),
  61. UploadId: aws.String(uploadID),
  62. }, parts)
  63. glog.V(2).Info("CompleteMultipartUploadHandler", string(s3err.EncodeXMLResponse(response)), errCode)
  64. if errCode != s3err.ErrNone {
  65. s3err.WriteErrorResponse(w, r, errCode)
  66. return
  67. }
  68. writeSuccessResponseXML(w, r, response)
  69. }
  70. // AbortMultipartUploadHandler - Aborts multipart upload.
  71. func (s3a *S3ApiServer) AbortMultipartUploadHandler(w http.ResponseWriter, r *http.Request) {
  72. bucket, object := xhttp.GetBucketAndObject(r)
  73. // Get upload id.
  74. uploadID, _, _, _ := getObjectResources(r.URL.Query())
  75. response, errCode := s3a.abortMultipartUpload(&s3.AbortMultipartUploadInput{
  76. Bucket: aws.String(bucket),
  77. Key: objectKey(aws.String(object)),
  78. UploadId: aws.String(uploadID),
  79. })
  80. if errCode != s3err.ErrNone {
  81. s3err.WriteErrorResponse(w, r, errCode)
  82. return
  83. }
  84. glog.V(2).Info("AbortMultipartUploadHandler", string(s3err.EncodeXMLResponse(response)))
  85. writeSuccessResponseXML(w, r, response)
  86. }
  87. // ListMultipartUploadsHandler - Lists multipart uploads.
  88. func (s3a *S3ApiServer) ListMultipartUploadsHandler(w http.ResponseWriter, r *http.Request) {
  89. bucket, _ := xhttp.GetBucketAndObject(r)
  90. prefix, keyMarker, uploadIDMarker, delimiter, maxUploads, encodingType := getBucketMultipartResources(r.URL.Query())
  91. if maxUploads < 0 {
  92. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxUploads)
  93. return
  94. }
  95. if keyMarker != "" {
  96. // Marker not common with prefix is not implemented.
  97. if !strings.HasPrefix(keyMarker, prefix) {
  98. s3err.WriteErrorResponse(w, r, s3err.ErrNotImplemented)
  99. return
  100. }
  101. }
  102. response, errCode := s3a.listMultipartUploads(&s3.ListMultipartUploadsInput{
  103. Bucket: aws.String(bucket),
  104. Delimiter: aws.String(delimiter),
  105. EncodingType: aws.String(encodingType),
  106. KeyMarker: aws.String(keyMarker),
  107. MaxUploads: aws.Int64(int64(maxUploads)),
  108. Prefix: aws.String(prefix),
  109. UploadIdMarker: aws.String(uploadIDMarker),
  110. })
  111. glog.V(2).Infof("ListMultipartUploadsHandler %s errCode=%d", string(s3err.EncodeXMLResponse(response)), errCode)
  112. if errCode != s3err.ErrNone {
  113. s3err.WriteErrorResponse(w, r, errCode)
  114. return
  115. }
  116. // TODO handle encodingType
  117. writeSuccessResponseXML(w, r, response)
  118. }
  119. // ListObjectPartsHandler - Lists object parts in a multipart upload.
  120. func (s3a *S3ApiServer) ListObjectPartsHandler(w http.ResponseWriter, r *http.Request) {
  121. bucket, object := xhttp.GetBucketAndObject(r)
  122. uploadID, partNumberMarker, maxParts, _ := getObjectResources(r.URL.Query())
  123. if partNumberMarker < 0 {
  124. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidPartNumberMarker)
  125. return
  126. }
  127. if maxParts < 0 {
  128. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxParts)
  129. return
  130. }
  131. response, errCode := s3a.listObjectParts(&s3.ListPartsInput{
  132. Bucket: aws.String(bucket),
  133. Key: objectKey(aws.String(object)),
  134. MaxParts: aws.Int64(int64(maxParts)),
  135. PartNumberMarker: aws.Int64(int64(partNumberMarker)),
  136. UploadId: aws.String(uploadID),
  137. })
  138. if errCode != s3err.ErrNone {
  139. s3err.WriteErrorResponse(w, r, errCode)
  140. return
  141. }
  142. glog.V(2).Infof("ListObjectPartsHandler %s count=%d", string(s3err.EncodeXMLResponse(response)), len(response.Part))
  143. writeSuccessResponseXML(w, r, response)
  144. }
  145. // PutObjectPartHandler - Put an object part in a multipart upload.
  146. func (s3a *S3ApiServer) PutObjectPartHandler(w http.ResponseWriter, r *http.Request) {
  147. bucket, _ := xhttp.GetBucketAndObject(r)
  148. uploadID := r.URL.Query().Get("uploadId")
  149. exists, err := s3a.exists(s3a.genUploadsFolder(bucket), uploadID, true)
  150. if !exists {
  151. s3err.WriteErrorResponse(w, r, s3err.ErrNoSuchUpload)
  152. return
  153. }
  154. partIDString := r.URL.Query().Get("partNumber")
  155. partID, err := strconv.Atoi(partIDString)
  156. if err != nil {
  157. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidPart)
  158. return
  159. }
  160. if partID > globalMaxPartID {
  161. s3err.WriteErrorResponse(w, r, s3err.ErrInvalidMaxParts)
  162. return
  163. }
  164. dataReader := r.Body
  165. if s3a.iam.isEnabled() {
  166. rAuthType := getRequestAuthType(r)
  167. var s3ErrCode s3err.ErrorCode
  168. switch rAuthType {
  169. case authTypeStreamingSigned:
  170. dataReader, s3ErrCode = s3a.iam.newSignV4ChunkedReader(r)
  171. case authTypeSignedV2, authTypePresignedV2:
  172. _, s3ErrCode = s3a.iam.isReqAuthenticatedV2(r)
  173. case authTypePresigned, authTypeSigned:
  174. _, s3ErrCode = s3a.iam.reqSignatureV4Verify(r)
  175. }
  176. if s3ErrCode != s3err.ErrNone {
  177. s3err.WriteErrorResponse(w, r, s3ErrCode)
  178. return
  179. }
  180. }
  181. defer dataReader.Close()
  182. glog.V(2).Infof("PutObjectPartHandler %s %s %04d", bucket, uploadID, partID)
  183. uploadUrl := fmt.Sprintf("http://%s%s/%s/%04d.part?collection=%s",
  184. s3a.option.Filer.ToHttpAddress(), s3a.genUploadsFolder(bucket), uploadID, partID, bucket)
  185. if partID == 1 && r.Header.Get("Content-Type") == "" {
  186. dataReader = mimeDetect(r, dataReader)
  187. }
  188. etag, errCode := s3a.putToFiler(r, uploadUrl, dataReader)
  189. if errCode != s3err.ErrNone {
  190. s3err.WriteErrorResponse(w, r, errCode)
  191. return
  192. }
  193. setEtag(w, etag)
  194. writeSuccessResponseEmpty(w, r)
  195. }
  196. func (s3a *S3ApiServer) genUploadsFolder(bucket string) string {
  197. return fmt.Sprintf("%s/%s/.uploads", s3a.option.BucketsPath, bucket)
  198. }
  199. // Parse bucket url queries for ?uploads
  200. func getBucketMultipartResources(values url.Values) (prefix, keyMarker, uploadIDMarker, delimiter string, maxUploads int, encodingType string) {
  201. prefix = values.Get("prefix")
  202. keyMarker = values.Get("key-marker")
  203. uploadIDMarker = values.Get("upload-id-marker")
  204. delimiter = values.Get("delimiter")
  205. if values.Get("max-uploads") != "" {
  206. maxUploads, _ = strconv.Atoi(values.Get("max-uploads"))
  207. } else {
  208. maxUploads = maxUploadsList
  209. }
  210. encodingType = values.Get("encoding-type")
  211. return
  212. }
  213. // Parse object url queries
  214. func getObjectResources(values url.Values) (uploadID string, partNumberMarker, maxParts int, encodingType string) {
  215. uploadID = values.Get("uploadId")
  216. partNumberMarker, _ = strconv.Atoi(values.Get("part-number-marker"))
  217. if values.Get("max-parts") != "" {
  218. maxParts, _ = strconv.Atoi(values.Get("max-parts"))
  219. } else {
  220. maxParts = maxPartsList
  221. }
  222. encodingType = values.Get("encoding-type")
  223. return
  224. }
  225. func xmlDecoder(body io.Reader, v interface{}, size int64) error {
  226. var lbody io.Reader
  227. if size > 0 {
  228. lbody = io.LimitReader(body, size)
  229. } else {
  230. lbody = body
  231. }
  232. d := xml.NewDecoder(lbody)
  233. d.CharsetReader = func(label string, input io.Reader) (io.Reader, error) {
  234. return input, nil
  235. }
  236. return d.Decode(v)
  237. }
  238. type CompleteMultipartUpload struct {
  239. Parts []CompletedPart `xml:"Part"`
  240. }
  241. type CompletedPart struct {
  242. ETag string
  243. PartNumber int
  244. }