command_bucket_create.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "os"
  8. "time"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. )
  11. func init() {
  12. Commands = append(Commands, &commandBucketCreate{})
  13. }
  14. type commandBucketCreate struct {
  15. }
  16. func (c *commandBucketCreate) Name() string {
  17. return "bucket.create"
  18. }
  19. func (c *commandBucketCreate) Help() string {
  20. return `create a bucket with a given name
  21. Example:
  22. bucket.create -name <bucket_name> -replication 001
  23. `
  24. }
  25. func (c *commandBucketCreate) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  26. bucketCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  27. bucketName := bucketCommand.String("name", "", "bucket name")
  28. replication := bucketCommand.String("replication", "", "replication setting for the bucket")
  29. if err = bucketCommand.Parse(args); err != nil {
  30. return nil
  31. }
  32. if *bucketName == "" {
  33. return fmt.Errorf("empty bucket name")
  34. }
  35. filerServer, filerPort, _, parseErr := commandEnv.parseUrl(findInputDirectory(bucketCommand.Args()))
  36. if parseErr != nil {
  37. return parseErr
  38. }
  39. err = commandEnv.withFilerClient(filerServer, filerPort, func(client filer_pb.SeaweedFilerClient) error {
  40. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  41. if err != nil {
  42. return fmt.Errorf("get filer %s:%d configuration: %v", filerServer, filerPort, err)
  43. }
  44. filerBucketsPath := resp.DirBuckets
  45. println("create bucket under", filerBucketsPath)
  46. entry := &filer_pb.Entry{
  47. Name: *bucketName,
  48. IsDirectory: true,
  49. Attributes: &filer_pb.FuseAttributes{
  50. Mtime: time.Now().Unix(),
  51. Crtime: time.Now().Unix(),
  52. FileMode: uint32(0777 | os.ModeDir),
  53. Collection: *bucketName,
  54. Replication: *replication,
  55. },
  56. }
  57. if err := filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
  58. Directory: filerBucketsPath,
  59. Entry: entry,
  60. }); err != nil {
  61. return err
  62. }
  63. println("created bucket", *bucketName)
  64. return nil
  65. })
  66. return err
  67. }