s3.go 2.9 KB

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