forked from koblas/s3-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_du.go
50 lines (40 loc) · 996 Bytes
/
cmd_du.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/urfave/cli/v2"
)
func GetUsage(config *Config, c *cli.Context) error {
args := c.Args().Slice()
svc := SessionNew(config)
// If we're not passed any args, we're going to do all S3 buckets
if len(args) == 0 {
var params *s3.ListBucketsInput
resp, err := svc.ListBuckets(params)
if err != nil {
return err
}
for _, bucket := range resp.Buckets {
args = append(args, fmt.Sprintf("s3://%s", *bucket.Name))
}
}
// Get the usage for the buckets
for _, arg := range args {
// Only do usage on S3 buckets
u, err := FileURINew(arg)
if err != nil || u.Scheme != "s3" {
continue
}
var (
bucketSize, bucketObjs int64
)
remotePager(config, svc, arg, false, func(page *s3.ListObjectsV2Output) {
for _, obj := range page.Contents {
bucketSize += *obj.Size
bucketObjs += 1
}
})
fmt.Printf("%d %d objects %s\n", bucketSize, bucketObjs, arg)
}
return nil
}