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.

138 lines
6.1 KiB

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