command_s3_bucket_create.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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) HasTag(CommandTag) 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. }