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.

116 lines
4.4 KiB

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