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.

110 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. // CopyObjectPart
  38. bucket.Methods("PUT").Path("/{object:.+}").HeadersRegexp("X-Amz-Copy-Source", ".*?(\\/|%2F).*?").HandlerFunc(s3a.CopyObjectPartHandler).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}")
  39. // PutObjectPart
  40. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(s3a.PutObjectPartHandler).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}")
  41. // CompleteMultipartUpload
  42. bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(s3a.CompleteMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}")
  43. // NewMultipartUpload
  44. bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(s3a.NewMultipartUploadHandler).Queries("uploads", "")
  45. // AbortMultipartUpload
  46. bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(s3a.AbortMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}")
  47. // ListObjectParts
  48. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(s3a.ListObjectPartsHandler).Queries("uploadId", "{uploadId:.*}")
  49. // ListMultipartUploads
  50. bucket.Methods("GET").HandlerFunc(s3a.ListMultipartUploadsHandler).Queries("uploads", "")
  51. // CopyObject
  52. bucket.Methods("PUT").Path("/{object:.+}").HeadersRegexp("X-Amz-Copy-Source", ".*?(\\/|%2F).*?").HandlerFunc(s3a.CopyObjectHandler)
  53. // PutObject
  54. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(s3a.PutObjectHandler)
  55. // PutBucket
  56. bucket.Methods("PUT").HandlerFunc(s3a.PutBucketHandler)
  57. // DeleteObject
  58. bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(s3a.DeleteObjectHandler)
  59. // DeleteBucket
  60. bucket.Methods("DELETE").HandlerFunc(s3a.DeleteBucketHandler)
  61. // ListObjectsV2
  62. bucket.Methods("GET").HandlerFunc(s3a.ListObjectsV2Handler).Queries("list-type", "2")
  63. // GetObject, but directory listing is not supported
  64. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(s3a.GetObjectHandler)
  65. // ListObjectsV1 (Legacy)
  66. bucket.Methods("GET").HandlerFunc(s3a.ListObjectsV1Handler)
  67. // DeleteMultipleObjects
  68. bucket.Methods("POST").HandlerFunc(s3a.DeleteMultipleObjectsHandler).Queries("delete", "")
  69. /*
  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. }