s3api_server.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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(s3a.iam.Auth(s3a.HeadObjectHandler, ACTION_READ))
  43. // HeadBucket
  44. bucket.Methods("HEAD").HandlerFunc(s3a.iam.Auth(s3a.HeadBucketHandler, ACTION_ADMIN))
  45. // CopyObjectPart
  46. 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:.*}")
  47. // PutObjectPart
  48. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(s3a.iam.Auth(s3a.PutObjectPartHandler, ACTION_WRITE)).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}")
  49. // CompleteMultipartUpload
  50. bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(s3a.iam.Auth(s3a.CompleteMultipartUploadHandler, ACTION_WRITE)).Queries("uploadId", "{uploadId:.*}")
  51. // NewMultipartUpload
  52. bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(s3a.iam.Auth(s3a.NewMultipartUploadHandler, ACTION_WRITE)).Queries("uploads", "")
  53. // AbortMultipartUpload
  54. bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(s3a.iam.Auth(s3a.AbortMultipartUploadHandler, ACTION_WRITE)).Queries("uploadId", "{uploadId:.*}")
  55. // ListObjectParts
  56. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(s3a.iam.Auth(s3a.ListObjectPartsHandler, ACTION_WRITE)).Queries("uploadId", "{uploadId:.*}")
  57. // ListMultipartUploads
  58. bucket.Methods("GET").HandlerFunc(s3a.iam.Auth(s3a.ListMultipartUploadsHandler, ACTION_WRITE)).Queries("uploads", "")
  59. // CopyObject
  60. bucket.Methods("PUT").Path("/{object:.+}").HeadersRegexp("X-Amz-Copy-Source", ".*?(\\/|%2F).*?").HandlerFunc(s3a.iam.Auth(s3a.CopyObjectHandler, ACTION_WRITE))
  61. // PutObject
  62. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(s3a.iam.Auth(s3a.PutObjectHandler, ACTION_WRITE))
  63. // PutBucket
  64. bucket.Methods("PUT").HandlerFunc(s3a.iam.Auth(s3a.PutBucketHandler, ACTION_ADMIN))
  65. // DeleteObject
  66. bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(s3a.iam.Auth(s3a.DeleteObjectHandler, ACTION_WRITE))
  67. // DeleteBucket
  68. bucket.Methods("DELETE").HandlerFunc(s3a.iam.Auth(s3a.DeleteBucketHandler, ACTION_WRITE))
  69. // ListObjectsV2
  70. bucket.Methods("GET").HandlerFunc(s3a.iam.Auth(s3a.ListObjectsV2Handler, ACTION_READ)).Queries("list-type", "2")
  71. // GetObject, but directory listing is not supported
  72. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(s3a.iam.Auth(s3a.GetObjectHandler, ACTION_READ))
  73. // ListObjectsV1 (Legacy)
  74. bucket.Methods("GET").HandlerFunc(s3a.iam.Auth(s3a.ListObjectsV1Handler, ACTION_READ))
  75. // DeleteMultipleObjects
  76. bucket.Methods("POST").HandlerFunc(s3a.iam.Auth(s3a.DeleteMultipleObjectsHandler, ACTION_WRITE)).Queries("delete", "")
  77. /*
  78. // not implemented
  79. // GetBucketLocation
  80. bucket.Methods("GET").HandlerFunc(s3a.GetBucketLocationHandler).Queries("location", "")
  81. // GetBucketPolicy
  82. bucket.Methods("GET").HandlerFunc(s3a.GetBucketPolicyHandler).Queries("policy", "")
  83. // GetObjectACL
  84. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(s3a.GetObjectACLHandler).Queries("acl", "")
  85. // GetBucketACL
  86. bucket.Methods("GET").HandlerFunc(s3a.GetBucketACLHandler).Queries("acl", "")
  87. // PutBucketPolicy
  88. bucket.Methods("PUT").HandlerFunc(s3a.PutBucketPolicyHandler).Queries("policy", "")
  89. // DeleteBucketPolicy
  90. bucket.Methods("DELETE").HandlerFunc(s3a.DeleteBucketPolicyHandler).Queries("policy", "")
  91. // PostPolicy
  92. bucket.Methods("POST").HeadersRegexp("Content-Type", "multipart/form-data*").HandlerFunc(s3a.PostPolicyBucketHandler)
  93. */
  94. }
  95. // ListBuckets
  96. apiRouter.Methods("GET").Path("/").HandlerFunc(s3a.iam.Auth(s3a.ListBucketsHandler, ACTION_ADMIN))
  97. // NotFound
  98. apiRouter.NotFoundHandler = http.HandlerFunc(notFoundHandler)
  99. }