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.

169 lines
7.7 KiB

5 years ago
5 years ago
5 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
3 years ago
4 years ago
  1. package s3api
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/pb"
  5. "github.com/chrislusf/seaweedfs/weed/util"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/filer"
  10. . "github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
  11. "github.com/chrislusf/seaweedfs/weed/s3api/s3err"
  12. "github.com/gorilla/mux"
  13. "google.golang.org/grpc"
  14. )
  15. type S3ApiServerOption struct {
  16. Filer pb.ServerAddress
  17. Port int
  18. Config string
  19. DomainName string
  20. BucketsPath string
  21. GrpcDialOption grpc.DialOption
  22. AllowEmptyFolder bool
  23. }
  24. type S3ApiServer struct {
  25. option *S3ApiServerOption
  26. iam *IdentityAccessManagement
  27. randomClientId int32
  28. }
  29. func NewS3ApiServer(router *mux.Router, option *S3ApiServerOption) (s3ApiServer *S3ApiServer, err error) {
  30. s3ApiServer = &S3ApiServer{
  31. option: option,
  32. iam: NewIdentityAccessManagement(option),
  33. randomClientId: util.RandomInt32(),
  34. }
  35. s3ApiServer.registerRouter(router)
  36. go s3ApiServer.subscribeMetaEvents("s3", filer.IamConfigDirecotry+"/"+filer.IamIdentityFile, time.Now().UnixNano())
  37. return s3ApiServer, nil
  38. }
  39. func (s3a *S3ApiServer) registerRouter(router *mux.Router) {
  40. // API Router
  41. apiRouter := router.PathPrefix("/").Subrouter()
  42. // Readiness Probe
  43. apiRouter.Methods("GET").Path("/status").HandlerFunc(s3a.StatusHandler)
  44. var routers []*mux.Router
  45. if s3a.option.DomainName != "" {
  46. domainNames := strings.Split(s3a.option.DomainName, ",")
  47. for _, domainName := range domainNames {
  48. routers = append(routers, apiRouter.Host(
  49. fmt.Sprintf("%s.%s:%d", "{bucket:.+}", domainName, s3a.option.Port)).Subrouter())
  50. routers = append(routers, apiRouter.Host(
  51. fmt.Sprintf("%s.%s", "{bucket:.+}", domainName)).Subrouter())
  52. }
  53. }
  54. routers = append(routers, apiRouter.PathPrefix("/{bucket}").Subrouter())
  55. for _, bucket := range routers {
  56. // HeadObject
  57. bucket.Methods("HEAD").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.HeadObjectHandler, ACTION_READ), "GET"))
  58. // HeadBucket
  59. bucket.Methods("HEAD").HandlerFunc(track(s3a.iam.Auth(s3a.HeadBucketHandler, ACTION_READ), "GET"))
  60. // CopyObjectPart
  61. bucket.Methods("PUT").Path("/{object:.+}").HeadersRegexp("X-Amz-Copy-Source", `.*?(\/|%2F).*?`).HandlerFunc(track(s3a.iam.Auth(s3a.CopyObjectPartHandler, ACTION_WRITE), "PUT")).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}")
  62. // PutObjectPart
  63. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.PutObjectPartHandler, ACTION_WRITE), "PUT")).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}")
  64. // CompleteMultipartUpload
  65. bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.CompleteMultipartUploadHandler, ACTION_WRITE), "POST")).Queries("uploadId", "{uploadId:.*}")
  66. // NewMultipartUpload
  67. bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.NewMultipartUploadHandler, ACTION_WRITE), "POST")).Queries("uploads", "")
  68. // AbortMultipartUpload
  69. bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.AbortMultipartUploadHandler, ACTION_WRITE), "DELETE")).Queries("uploadId", "{uploadId:.*}")
  70. // ListObjectParts
  71. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.ListObjectPartsHandler, ACTION_READ), "GET")).Queries("uploadId", "{uploadId:.*}")
  72. // ListMultipartUploads
  73. bucket.Methods("GET").HandlerFunc(track(s3a.iam.Auth(s3a.ListMultipartUploadsHandler, ACTION_READ), "GET")).Queries("uploads", "")
  74. // GetObjectTagging
  75. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.GetObjectTaggingHandler, ACTION_READ), "GET")).Queries("tagging", "")
  76. // PutObjectTagging
  77. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.PutObjectTaggingHandler, ACTION_TAGGING), "PUT")).Queries("tagging", "")
  78. // DeleteObjectTagging
  79. bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.DeleteObjectTaggingHandler, ACTION_TAGGING), "DELETE")).Queries("tagging", "")
  80. // PutObjectACL
  81. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.PutObjectAclHandler, ACTION_WRITE), "PUT")).Queries("acl", "")
  82. // PutObjectRetention
  83. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.PutObjectRetentionHandler, ACTION_WRITE), "PUT")).Queries("retention", "")
  84. // PutObjectLegalHold
  85. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.PutObjectLegalHoldHandler, ACTION_WRITE), "PUT")).Queries("legal-hold", "")
  86. // PutObjectLockConfiguration
  87. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.PutObjectLockConfigurationHandler, ACTION_WRITE), "PUT")).Queries("object-lock", "")
  88. // CopyObject
  89. bucket.Methods("PUT").Path("/{object:.+}").HeadersRegexp("X-Amz-Copy-Source", ".*?(\\/|%2F).*?").HandlerFunc(track(s3a.iam.Auth(s3a.CopyObjectHandler, ACTION_WRITE), "COPY"))
  90. // PutObject
  91. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.PutObjectHandler, ACTION_WRITE), "PUT"))
  92. // DeleteObject
  93. bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.DeleteObjectHandler, ACTION_WRITE), "DELETE"))
  94. // ListObjectsV2
  95. bucket.Methods("GET").HandlerFunc(track(s3a.iam.Auth(s3a.ListObjectsV2Handler, ACTION_LIST), "LIST")).Queries("list-type", "2")
  96. // GetObject, but directory listing is not supported
  97. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(track(s3a.iam.Auth(s3a.GetObjectHandler, ACTION_READ), "GET"))
  98. // PostPolicy
  99. bucket.Methods("POST").HeadersRegexp("Content-Type", "multipart/form-data*").HandlerFunc(track(s3a.iam.Auth(s3a.PostPolicyBucketHandler, ACTION_WRITE), "POST"))
  100. // DeleteMultipleObjects
  101. bucket.Methods("POST").HandlerFunc(track(s3a.iam.Auth(s3a.DeleteMultipleObjectsHandler, ACTION_WRITE), "DELETE")).Queries("delete", "")
  102. // GetBucketACL
  103. bucket.Methods("GET").HandlerFunc(s3a.iam.Auth(s3a.GetBucketAclHandler, ACTION_READ)).Queries("acl", "")
  104. // GetObjectACL
  105. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(s3a.iam.Auth(s3a.GetObjectAclHandler, ACTION_READ)).Queries("acl", "")
  106. // GetBucketLifecycleConfiguration
  107. bucket.Methods("GET").HandlerFunc(s3a.iam.Auth(s3a.GetBucketLifecycleConfigurationHandler, ACTION_READ)).Queries("lifecycle", "")
  108. // PutBucketLifecycleConfiguration
  109. bucket.Methods("PUT").HandlerFunc(s3a.iam.Auth(s3a.PutBucketLifecycleConfigurationHandler, ACTION_WRITE)).Queries("lifecycle", "")
  110. // DeleteBucketLifecycleConfiguration
  111. bucket.Methods("DELETE").HandlerFunc(s3a.iam.Auth(s3a.DeleteBucketLifecycleHandler, ACTION_WRITE)).Queries("lifecycle", "")
  112. // ListObjectsV1 (Legacy)
  113. bucket.Methods("GET").HandlerFunc(track(s3a.iam.Auth(s3a.ListObjectsV1Handler, ACTION_LIST), "LIST"))
  114. // PutBucket
  115. bucket.Methods("PUT").HandlerFunc(track(s3a.PutBucketHandler, "PUT"))
  116. // DeleteBucket
  117. bucket.Methods("DELETE").HandlerFunc(track(s3a.iam.Auth(s3a.DeleteBucketHandler, ACTION_WRITE), "DELETE"))
  118. /*
  119. // not implemented
  120. // GetBucketLocation
  121. bucket.Methods("GET").HandlerFunc(s3a.GetBucketLocationHandler).Queries("location", "")
  122. // GetBucketPolicy
  123. bucket.Methods("GET").HandlerFunc(s3a.GetBucketPolicyHandler).Queries("policy", "")
  124. // GetObjectACL
  125. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(s3a.GetObjectACLHandler).Queries("acl", "")
  126. // PutBucketPolicy
  127. bucket.Methods("PUT").HandlerFunc(s3a.PutBucketPolicyHandler).Queries("policy", "")
  128. // DeleteBucketPolicy
  129. bucket.Methods("DELETE").HandlerFunc(s3a.DeleteBucketPolicyHandler).Queries("policy", "")
  130. */
  131. }
  132. // ListBuckets
  133. apiRouter.Methods("GET").Path("/").HandlerFunc(track(s3a.ListBucketsHandler, "LIST"))
  134. // NotFound
  135. apiRouter.NotFoundHandler = http.HandlerFunc(s3err.NotFoundHandler)
  136. }