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.

76 lines
2.2 KiB

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