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.

117 lines
4.4 KiB

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