command_s3_bucket_list.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "math"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  9. )
  10. func init() {
  11. Commands = append(Commands, &commandS3BucketList{})
  12. }
  13. type commandS3BucketList struct {
  14. }
  15. func (c *commandS3BucketList) Name() string {
  16. return "s3.bucket.list"
  17. }
  18. func (c *commandS3BucketList) Help() string {
  19. return `list all buckets
  20. `
  21. }
  22. func (c *commandS3BucketList) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  23. bucketCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  24. if err = bucketCommand.Parse(args); err != nil {
  25. return nil
  26. }
  27. // collect collection information
  28. topologyInfo, _, err := collectTopologyInfo(commandEnv, 0)
  29. if err != nil {
  30. return err
  31. }
  32. collectionInfos := make(map[string]*CollectionInfo)
  33. collectCollectionInfo(topologyInfo, collectionInfos)
  34. _, parseErr := commandEnv.parseUrl(findInputDirectory(bucketCommand.Args()))
  35. if parseErr != nil {
  36. return parseErr
  37. }
  38. var filerBucketsPath string
  39. filerBucketsPath, err = readFilerBucketsPath(commandEnv)
  40. if err != nil {
  41. return fmt.Errorf("read buckets: %v", err)
  42. }
  43. err = filer_pb.List(commandEnv, filerBucketsPath, "", func(entry *filer_pb.Entry, isLast bool) error {
  44. if !entry.IsDirectory {
  45. return nil
  46. }
  47. collection := getCollectionName(commandEnv, entry.Name)
  48. var collectionSize, fileCount float64
  49. if collectionInfo, found := collectionInfos[collection]; found {
  50. collectionSize = collectionInfo.Size
  51. fileCount = collectionInfo.FileCount - collectionInfo.DeleteCount
  52. }
  53. fmt.Fprintf(writer, " %s\tsize:%.0f\tchunk:%.0f", entry.Name, collectionSize, fileCount)
  54. if entry.Quota > 0 {
  55. fmt.Fprintf(writer, "\tquota:%d\tusage:%.2f%%", entry.Quota, float64(collectionSize)*100/float64(entry.Quota))
  56. }
  57. fmt.Fprintln(writer)
  58. return nil
  59. }, "", false, math.MaxUint32)
  60. if err != nil {
  61. return fmt.Errorf("list buckets under %v: %v", filerBucketsPath, err)
  62. }
  63. return err
  64. }
  65. func readFilerBucketsPath(filerClient filer_pb.FilerClient) (filerBucketsPath string, err error) {
  66. err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  67. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  68. if err != nil {
  69. return fmt.Errorf("get filer configuration: %v", err)
  70. }
  71. filerBucketsPath = resp.DirBuckets
  72. return nil
  73. })
  74. return filerBucketsPath, err
  75. }