s3.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package command
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/chrislusf/seaweedfs/weed/security"
  6. "github.com/spf13/viper"
  7. "fmt"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. "github.com/chrislusf/seaweedfs/weed/s3api"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. "github.com/gorilla/mux"
  12. )
  13. var (
  14. s3StandaloneOptions S3Options
  15. )
  16. type S3Options struct {
  17. filer *string
  18. filerBucketsPath *string
  19. port *int
  20. domainName *string
  21. tlsPrivateKey *string
  22. tlsCertificate *string
  23. }
  24. func init() {
  25. cmdS3.Run = runS3 // break init cycle
  26. s3StandaloneOptions.filer = cmdS3.Flag.String("filer", "localhost:8888", "filer server address")
  27. s3StandaloneOptions.filerBucketsPath = cmdS3.Flag.String("filer.dir.buckets", "/buckets", "folder on filer to store all buckets")
  28. s3StandaloneOptions.port = cmdS3.Flag.Int("port", 8333, "s3 server http listen port")
  29. s3StandaloneOptions.domainName = cmdS3.Flag.String("domainName", "", "suffix of the host name, {bucket}.{domainName}")
  30. s3StandaloneOptions.tlsPrivateKey = cmdS3.Flag.String("key.file", "", "path to the TLS private key file")
  31. s3StandaloneOptions.tlsCertificate = cmdS3.Flag.String("cert.file", "", "path to the TLS certificate file")
  32. }
  33. var cmdS3 = &Command{
  34. UsageLine: "s3 -port=8333 -filer=<ip:port>",
  35. Short: "start a s3 API compatible server that is backed by a filer",
  36. Long: `start a s3 API compatible server that is backed by a filer.
  37. `,
  38. }
  39. func runS3(cmd *Command, args []string) bool {
  40. util.LoadConfiguration("security", false)
  41. return s3StandaloneOptions.startS3Server()
  42. }
  43. func (s3opt *S3Options) startS3Server() bool {
  44. filerGrpcAddress, err := parseFilerGrpcAddress(*s3opt.filer)
  45. if err != nil {
  46. glog.Fatal(err)
  47. return false
  48. }
  49. router := mux.NewRouter().SkipClean(true)
  50. _, s3ApiServer_err := s3api.NewS3ApiServer(router, &s3api.S3ApiServerOption{
  51. Filer: *s3opt.filer,
  52. FilerGrpcAddress: filerGrpcAddress,
  53. DomainName: *s3opt.domainName,
  54. BucketsPath: *s3opt.filerBucketsPath,
  55. GrpcDialOption: security.LoadClientTLS(viper.Sub("grpc"), "client"),
  56. })
  57. if s3ApiServer_err != nil {
  58. glog.Fatalf("S3 API Server startup error: %v", s3ApiServer_err)
  59. }
  60. httpS := &http.Server{Handler: router}
  61. listenAddress := fmt.Sprintf(":%d", *s3opt.port)
  62. s3ApiListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
  63. if err != nil {
  64. glog.Fatalf("S3 API Server listener on %s error: %v", listenAddress, err)
  65. }
  66. if *s3opt.tlsPrivateKey != "" {
  67. glog.V(0).Infof("Start Seaweed S3 API Server %s at https port %d", util.VERSION, *s3opt.port)
  68. if err = httpS.ServeTLS(s3ApiListener, *s3opt.tlsCertificate, *s3opt.tlsPrivateKey); err != nil {
  69. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  70. }
  71. } else {
  72. glog.V(0).Infof("Start Seaweed S3 API Server %s at http port %d", util.VERSION, *s3opt.port)
  73. if err = httpS.Serve(s3ApiListener); err != nil {
  74. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  75. }
  76. }
  77. return true
  78. }