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.

111 lines
4.1 KiB

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