forked from koblas/s3-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_del.go
90 lines (74 loc) · 2.75 KB
/
cmd_del.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/urfave/cli"
)
// TODO: Handle --recusrive
func DeleteObjects(config *Config, c *cli.Context) error {
args := c.Args()
svc := SessionNew(config)
buckets := make(map[string][]*s3.ObjectIdentifier, 0)
for _, path := range args {
u, err := FileURINew(args[0])
if err != nil || u.Scheme != "s3" {
return fmt.Errorf("rm requires buckets to be prefixed with s3://")
}
if (u.Path == "" || strings.HasSuffix(u.Path, "/")) && !config.Recursive {
return fmt.Errorf("Parameter problem: Expecting S3 URI with a filename or --recursive: %s", path)
}
objects := buckets[u.Bucket]
if objects == nil {
objects = make([]*s3.ObjectIdentifier, 0)
}
buckets[u.Bucket] = append(objects, &s3.ObjectIdentifier{ Key: u.Key() })
}
// FIXME: Limited to 1000 objects, that's that shouldn't be an issue, but ...
for bucket, objects := range buckets {
bsvc, err := SessionForBucket(svc, bucket)
if err != nil {
return err
}
if config.Recursive {
for _, obj := range objects {
uri := fmt.Sprintf("s3://%s/%s", bucket, *obj.Key)
remotePager(config, svc, uri, false, func (page *s3.ListObjectsV2Output) {
olist := make([]*s3.ObjectIdentifier, 0)
for _, item := range page.Contents {
olist = append(olist, &s3.ObjectIdentifier{ Key: item.Key })
fmt.Printf("delete: s3://%s/%s\n", bucket, *item.Key)
}
if !config.DryRun {
params := &s3.DeleteObjectsInput{
Bucket: aws.String(bucket), // Required
Delete: &s3.Delete{
Objects: olist,
},
}
_, err := bsvc.DeleteObjects(params)
if err != nil {
fmt.Println("Error removing")
}
}
})
}
} else if !config.DryRun {
params := &s3.DeleteObjectsInput{
Bucket: aws.String(bucket), // Required
Delete: &s3.Delete{ // Required
Objects: objects,
},
}
_, err := bsvc.DeleteObjects(params)
if err != nil {
return err
}
}
for _, objs := range objects {
fmt.Printf("delete: s3://%s/%s\n", bucket, *objs.Key)
}
}
return nil
}