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.

85 lines
1.9 KiB

  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, &commandS3BucketCreate{})
  13. }
  14. type commandS3BucketCreate struct {
  15. }
  16. func (c *commandS3BucketCreate) Name() string {
  17. return "s3.bucket.create"
  18. }
  19. func (c *commandS3BucketCreate) Help() string {
  20. return `create a bucket with a given name
  21. Example:
  22. s3.bucket.create -name <bucket_name> -replication 001
  23. `
  24. }
  25. func (c *commandS3BucketCreate) 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, if not set "+
  29. "it will honor the value defined by the filer if it exists, "+
  30. "else it will honor the value defined on the master")
  31. if err = bucketCommand.Parse(args); err != nil {
  32. return nil
  33. }
  34. if *bucketName == "" {
  35. return fmt.Errorf("empty bucket name")
  36. }
  37. err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  38. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  39. if err != nil {
  40. return fmt.Errorf("get filer configuration: %v", err)
  41. }
  42. filerBucketsPath := resp.DirBuckets
  43. println("create bucket under", filerBucketsPath)
  44. entry := &filer_pb.Entry{
  45. Name: *bucketName,
  46. IsDirectory: true,
  47. Attributes: &filer_pb.FuseAttributes{
  48. Mtime: time.Now().Unix(),
  49. Crtime: time.Now().Unix(),
  50. FileMode: uint32(0777 | os.ModeDir),
  51. Collection: *bucketName,
  52. Replication: *replication,
  53. },
  54. }
  55. if err := filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
  56. Directory: filerBucketsPath,
  57. Entry: entry,
  58. }); err != nil {
  59. return err
  60. }
  61. println("created bucket", *bucketName)
  62. return nil
  63. })
  64. return err
  65. }