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.

115 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. "net/http"
  11. )
  12. type S3ApiServerOption struct {
  13. Filer string
  14. FilerGrpcAddress string
  15. DomainName string
  16. BucketsPath string
  17. }
  18. type S3ApiServer struct {
  19. option *S3ApiServerOption
  20. }
  21. func NewS3ApiServer(router *mux.Router, option *S3ApiServerOption) (s3ApiServer *S3ApiServer, err error) {
  22. s3ApiServer = &S3ApiServer{
  23. option: option,
  24. }
  25. s3ApiServer.registerRouter(router)
  26. return s3ApiServer, nil
  27. }
  28. func (s3a *S3ApiServer) registerRouter(router *mux.Router) {
  29. // API Router
  30. apiRouter := router.PathPrefix("/").Subrouter()
  31. var routers []*mux.Router
  32. if s3a.option.DomainName != "" {
  33. routers = append(routers, apiRouter.Host("{bucket:.+}."+s3a.option.DomainName).Subrouter())
  34. }
  35. routers = append(routers, apiRouter.PathPrefix("/{bucket}").Subrouter())
  36. for _, bucket := range routers {
  37. // HeadObject
  38. bucket.Methods("HEAD").Path("/{object:.+}").HandlerFunc(s3a.HeadObjectHandler)
  39. // HeadBucket
  40. bucket.Methods("HEAD").HandlerFunc(s3a.HeadBucketHandler)
  41. // PutObjectPart
  42. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(s3a.PutObjectPartHandler).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}")
  43. // CompleteMultipartUpload
  44. bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(s3a.CompleteMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}")
  45. // NewMultipartUpload
  46. bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(s3a.NewMultipartUploadHandler).Queries("uploads", "")
  47. // AbortMultipartUpload
  48. bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(s3a.AbortMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}")
  49. // ListObjectParts
  50. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(s3a.ListObjectPartsHandler).Queries("uploadId", "{uploadId:.*}")
  51. // ListMultipartUploads
  52. bucket.Methods("GET").HandlerFunc(s3a.ListMultipartUploadsHandler).Queries("uploads", "")
  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. // CopyObject
  71. bucket.Methods("PUT").Path("/{object:.+}").HeadersRegexp("X-Amz-Copy-Source", ".*?(\\/|%2F).*?").HandlerFunc(s3a.CopyObjectHandler)
  72. // CopyObjectPart
  73. bucket.Methods("PUT").Path("/{object:.+}").HeadersRegexp("X-Amz-Copy-Source", ".*?(\\/|%2F).*?").HandlerFunc(s3a.CopyObjectPartHandler).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}")
  74. // not implemented
  75. // GetBucketLocation
  76. bucket.Methods("GET").HandlerFunc(s3a.GetBucketLocationHandler).Queries("location", "")
  77. // GetBucketPolicy
  78. bucket.Methods("GET").HandlerFunc(s3a.GetBucketPolicyHandler).Queries("policy", "")
  79. // GetObjectACL
  80. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(s3a.GetObjectACLHandler).Queries("acl", "")
  81. // GetBucketACL
  82. bucket.Methods("GET").HandlerFunc(s3a.GetBucketACLHandler).Queries("acl", "")
  83. // PutBucketPolicy
  84. bucket.Methods("PUT").HandlerFunc(s3a.PutBucketPolicyHandler).Queries("policy", "")
  85. // DeleteBucketPolicy
  86. bucket.Methods("DELETE").HandlerFunc(s3a.DeleteBucketPolicyHandler).Queries("policy", "")
  87. // PostPolicy
  88. bucket.Methods("POST").HeadersRegexp("Content-Type", "multipart/form-data*").HandlerFunc(s3a.PostPolicyBucketHandler)
  89. */
  90. }
  91. // ListBuckets
  92. apiRouter.Methods("GET").Path("/").HandlerFunc(s3a.ListBucketsHandler)
  93. // NotFound
  94. apiRouter.NotFoundHandler = http.HandlerFunc(notFoundHandler)
  95. }