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.

136 lines
6.1 KiB

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