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.

99 lines
2.4 KiB

3 months ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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) HasTag(CommandTag) bool {
  23. return false
  24. }
  25. func (c *commandS3BucketList) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  26. bucketCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  27. if err = bucketCommand.Parse(args); err != nil {
  28. return nil
  29. }
  30. // collect collection information
  31. topologyInfo, _, err := collectTopologyInfo(commandEnv, 0)
  32. if err != nil {
  33. return err
  34. }
  35. collectionInfos := make(map[string]*CollectionInfo)
  36. collectCollectionInfo(topologyInfo, collectionInfos)
  37. _, parseErr := commandEnv.parseUrl(findInputDirectory(bucketCommand.Args()))
  38. if parseErr != nil {
  39. return parseErr
  40. }
  41. var filerBucketsPath string
  42. filerBucketsPath, err = readFilerBucketsPath(commandEnv)
  43. if err != nil {
  44. return fmt.Errorf("read buckets: %v", err)
  45. }
  46. err = filer_pb.List(commandEnv, filerBucketsPath, "", func(entry *filer_pb.Entry, isLast bool) error {
  47. if !entry.IsDirectory {
  48. return nil
  49. }
  50. collection := getCollectionName(commandEnv, entry.Name)
  51. var collectionSize, fileCount float64
  52. if collectionInfo, found := collectionInfos[collection]; found {
  53. collectionSize = collectionInfo.Size
  54. fileCount = collectionInfo.FileCount - collectionInfo.DeleteCount
  55. }
  56. fmt.Fprintf(writer, " %s\tsize:%.0f\tchunk:%.0f", entry.Name, collectionSize, fileCount)
  57. if entry.Quota > 0 {
  58. fmt.Fprintf(writer, "\tquota:%d\tusage:%.2f%%", entry.Quota, float64(collectionSize)*100/float64(entry.Quota))
  59. }
  60. fmt.Fprintln(writer)
  61. return nil
  62. }, "", false, math.MaxUint32)
  63. if err != nil {
  64. return fmt.Errorf("list buckets under %v: %v", filerBucketsPath, err)
  65. }
  66. return err
  67. }
  68. func readFilerBucketsPath(filerClient filer_pb.FilerClient) (filerBucketsPath string, err error) {
  69. err = filerClient.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  70. resp, err := client.GetFilerConfiguration(context.Background(), &filer_pb.GetFilerConfigurationRequest{})
  71. if err != nil {
  72. return fmt.Errorf("get filer configuration: %v", err)
  73. }
  74. filerBucketsPath = resp.DirBuckets
  75. return nil
  76. })
  77. return filerBucketsPath, err
  78. }