s3api_server.go 4.7 KB

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