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.

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