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.

89 lines
1.8 KiB

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