s3.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package command
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/chrislusf/seaweedfs/weed/pb"
  8. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  9. "github.com/chrislusf/seaweedfs/weed/security"
  10. "github.com/gorilla/mux"
  11. "github.com/chrislusf/seaweedfs/weed/glog"
  12. "github.com/chrislusf/seaweedfs/weed/s3api"
  13. "github.com/chrislusf/seaweedfs/weed/util"
  14. )
  15. var (
  16. s3StandaloneOptions S3Options
  17. )
  18. type S3Options struct {
  19. filer *string
  20. port *int
  21. config *string
  22. domainName *string
  23. tlsPrivateKey *string
  24. tlsCertificate *string
  25. }
  26. func init() {
  27. cmdS3.Run = runS3 // break init cycle
  28. s3StandaloneOptions.filer = cmdS3.Flag.String("filer", "localhost:8888", "filer server address")
  29. s3StandaloneOptions.port = cmdS3.Flag.Int("port", 8333, "s3 server http listen port")
  30. s3StandaloneOptions.domainName = cmdS3.Flag.String("domainName", "", "suffix of the host name, {bucket}.{domainName}")
  31. s3StandaloneOptions.config = cmdS3.Flag.String("config", "", "path to the config file")
  32. s3StandaloneOptions.tlsPrivateKey = cmdS3.Flag.String("key.file", "", "path to the TLS private key file")
  33. s3StandaloneOptions.tlsCertificate = cmdS3.Flag.String("cert.file", "", "path to the TLS certificate file")
  34. }
  35. var cmdS3 = &Command{
  36. UsageLine: "s3 [-port=8333] [-filer=<ip:port>] [-config=</path/to/config.json>]",
  37. Short: "start a s3 API compatible server that is backed by a filer",
  38. Long: `start a s3 API compatible server that is backed by a filer.
  39. By default, you can use any access key and secret key to access the S3 APIs.
  40. To enable credential based access, create a config.json file similar to this:
  41. {
  42. "identities": [
  43. {
  44. "name": "some_name",
  45. "credentials": [
  46. {
  47. "accessKey": "some_access_key1",
  48. "secretKey": "some_secret_key1"
  49. }
  50. ],
  51. "actions": [
  52. "Admin",
  53. "Read",
  54. "Write"
  55. ]
  56. },
  57. {
  58. "name": "some_read_only_user",
  59. "credentials": [
  60. {
  61. "accessKey": "some_access_key2",
  62. "secretKey": "some_secret_key2"
  63. }
  64. ],
  65. "actions": [
  66. "Read"
  67. ]
  68. },
  69. {
  70. "name": "some_normal_user",
  71. "credentials": [
  72. {
  73. "accessKey": "some_access_key3",
  74. "secretKey": "some_secret_key3"
  75. }
  76. ],
  77. "actions": [
  78. "Read",
  79. "Write"
  80. ]
  81. },
  82. {
  83. "name": "user_limited_to_bucket1",
  84. "credentials": [
  85. {
  86. "accessKey": "some_access_key4",
  87. "secretKey": "some_secret_key4"
  88. }
  89. ],
  90. "actions": [
  91. "Read:bucket1",
  92. "Write:bucket1"
  93. ]
  94. }
  95. ]
  96. }
  97. `,
  98. }
  99. func runS3(cmd *Command, args []string) bool {
  100. util.LoadConfiguration("security", false)
  101. return s3StandaloneOptions.startS3Server()
  102. }
  103. func (s3opt *S3Options) startS3Server() bool {
  104. filerGrpcAddress, err := pb.ParseFilerGrpcAddress(*s3opt.filer)
  105. if err != nil {
  106. glog.Fatal(err)
  107. return false
  108. }
  109. filerBucketsPath := "/buckets"
  110. grpcDialOption := security.LoadClientTLS(util.GetViper(), "grpc.client")
  111. for {
  112. err = pb.WithGrpcFilerClient(filerGrpcAddress, grpcDialOption, func(client filer_pb.SeaweedFilerClient) error {
  113. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  114. if err != nil {
  115. return fmt.Errorf("get filer %s configuration: %v", filerGrpcAddress, err)
  116. }
  117. filerBucketsPath = resp.DirBuckets
  118. glog.V(0).Infof("S3 read filer buckets dir: %s", filerBucketsPath)
  119. return nil
  120. })
  121. if err != nil {
  122. glog.V(0).Infof("wait to connect to filer %s grpc address %s", *s3opt.filer, filerGrpcAddress)
  123. time.Sleep(time.Second)
  124. } else {
  125. glog.V(0).Infof("connected to filer %s grpc address %s", *s3opt.filer, filerGrpcAddress)
  126. break
  127. }
  128. }
  129. router := mux.NewRouter().SkipClean(true)
  130. _, s3ApiServer_err := s3api.NewS3ApiServer(router, &s3api.S3ApiServerOption{
  131. Filer: *s3opt.filer,
  132. Port: *s3opt.port,
  133. FilerGrpcAddress: filerGrpcAddress,
  134. Config: *s3opt.config,
  135. DomainName: *s3opt.domainName,
  136. BucketsPath: filerBucketsPath,
  137. GrpcDialOption: grpcDialOption,
  138. })
  139. if s3ApiServer_err != nil {
  140. glog.Fatalf("S3 API Server startup error: %v", s3ApiServer_err)
  141. }
  142. httpS := &http.Server{Handler: router}
  143. listenAddress := fmt.Sprintf(":%d", *s3opt.port)
  144. s3ApiListener, err := util.NewListener(listenAddress, time.Duration(10)*time.Second)
  145. if err != nil {
  146. glog.Fatalf("S3 API Server listener on %s error: %v", listenAddress, err)
  147. }
  148. if *s3opt.tlsPrivateKey != "" {
  149. glog.V(0).Infof("Start Seaweed S3 API Server %s at https port %d", util.Version(), *s3opt.port)
  150. if err = httpS.ServeTLS(s3ApiListener, *s3opt.tlsCertificate, *s3opt.tlsPrivateKey); err != nil {
  151. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  152. }
  153. } else {
  154. glog.V(0).Infof("Start Seaweed S3 API Server %s at http port %d", util.Version(), *s3opt.port)
  155. if err = httpS.Serve(s3ApiListener); err != nil {
  156. glog.Fatalf("S3 API Server Fail to serve: %v", err)
  157. }
  158. }
  159. return true
  160. }