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

  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. // PutObject
  38. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(s3a.PutObjectHandler)
  39. // PutBucket
  40. bucket.Methods("PUT").HandlerFunc(s3a.PutBucketHandler)
  41. // HeadObject
  42. bucket.Methods("HEAD").Path("/{object:.+}").HandlerFunc(s3a.HeadObjectHandler)
  43. // HeadBucket
  44. bucket.Methods("HEAD").HandlerFunc(s3a.HeadBucketHandler)
  45. // PutObjectPart
  46. bucket.Methods("PUT").Path("/{object:.+}").HandlerFunc(s3a.PutObjectPartHandler).Queries("partNumber", "{partNumber:[0-9]+}", "uploadId", "{uploadId:.*}")
  47. // CompleteMultipartUpload
  48. bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(s3a.CompleteMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}")
  49. // NewMultipartUpload
  50. bucket.Methods("POST").Path("/{object:.+}").HandlerFunc(s3a.NewMultipartUploadHandler).Queries("uploads", "")
  51. // AbortMultipartUpload
  52. bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(s3a.AbortMultipartUploadHandler).Queries("uploadId", "{uploadId:.*}")
  53. // ListObjectParts
  54. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(s3a.ListObjectPartsHandler).Queries("uploadId", "{uploadId:.*}")
  55. // ListMultipartUploads
  56. bucket.Methods("GET").HandlerFunc(s3a.ListMultipartUploadsHandler).Queries("uploads", "")
  57. // DeleteObject
  58. bucket.Methods("DELETE").Path("/{object:.+}").HandlerFunc(s3a.DeleteObjectHandler)
  59. // DeleteBucket
  60. bucket.Methods("DELETE").HandlerFunc(s3a.DeleteBucketHandler)
  61. // GetObject, but directory listing is not supported
  62. bucket.Methods("GET").Path("/{object:.+}").HandlerFunc(s3a.GetObjectHandler)
  63. // ListObjectsV2
  64. bucket.Methods("GET").HandlerFunc(s3a.ListObjectsV2Handler).Queries("list-type", "2")
  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. }