command_s3_bucket_create.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "os"
  8. "time"
  9. "github.com/seaweedfs/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>
  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. if err = bucketCommand.Parse(args); err != nil {
  29. return nil
  30. }
  31. if *bucketName == "" {
  32. return fmt.Errorf("empty bucket name")
  33. }
  34. err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  35. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  36. if err != nil {
  37. return fmt.Errorf("get filer configuration: %v", err)
  38. }
  39. filerBucketsPath := resp.DirBuckets
  40. println("create bucket under", filerBucketsPath)
  41. entry := &filer_pb.Entry{
  42. Name: *bucketName,
  43. IsDirectory: true,
  44. Attributes: &filer_pb.FuseAttributes{
  45. Mtime: time.Now().Unix(),
  46. Crtime: time.Now().Unix(),
  47. FileMode: uint32(0777 | os.ModeDir),
  48. },
  49. }
  50. if err := filer_pb.CreateEntry(client, &filer_pb.CreateEntryRequest{
  51. Directory: filerBucketsPath,
  52. Entry: entry,
  53. }); err != nil {
  54. return err
  55. }
  56. println("created bucket", *bucketName)
  57. return nil
  58. })
  59. return err
  60. }