s3api_server.go 6.1 KB

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