command_s3_bucket_create.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  27. bucketCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  28. bucketName := bucketCommand.String("name", "", "bucket name")
  29. if err = bucketCommand.Parse(args); err != nil {
  30. return nil
  31. }
  32. if *bucketName == "" {
  33. return fmt.Errorf("empty bucket name")
  34. }
  35. err = s3bucket.VerifyS3BucketName(*bucketName)
  36. if err != nil {
  37. return err
  38. }
  39. err = commandEnv.WithFilerClient(false, 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 configuration: %v", 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. },
  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. }