-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3Cleaner.go
94 lines (77 loc) · 1.75 KB
/
s3Cleaner.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
91
92
93
94
package main
import (
"bufio"
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
var filePath string = "buckets.txt"
var region string = "us-east-1"
func deleteBucket(bucket *string) (ret bool) {
sess, _ := session.NewSession(&aws.Config{
Region: aws.String(region)},
)
svc := s3.New(sess)
objects, error := svc.ListObjects(&s3.ListObjectsInput{
Bucket: bucket,
})
fmt.Println(objects, error)
for i, v := range objects.Contents {
fmt.Println(i, v)
res, error := svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: bucket,
Key: v.Key,
})
fmt.Println(res, error)
}
versions, _ := svc.ListObjectVersions(&s3.ListObjectVersionsInput{
Bucket: bucket,
})
for i, v := range versions.Versions {
fmt.Println(i, v)
res, error := svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: bucket,
Key: v.Key,
VersionId: v.VersionId,
})
fmt.Println(res, error)
}
for i, v := range versions.DeleteMarkers {
fmt.Println(i, v)
res, error := svc.DeleteObject(&s3.DeleteObjectInput{
Bucket: bucket,
Key: v.Key,
VersionId: v.VersionId,
})
fmt.Println(res, error)
}
res, error := svc.DeleteBucket(&s3.DeleteBucketInput{
Bucket: bucket,
})
fmt.Println(res, error)
return true
}
func main() {
fmt.Println(os.Args)
if len(os.Args) == 2 {
filePath = os.Args[1]
}
if len(os.Args) == 3 {
filePath = os.Args[1]
region = os.Args[2]
}
fmt.Println("[File of bucket list]: " + filePath)
fmt.Println("[S3 region]: " + region)
f, error := os.Open(filePath)
if error != nil {
fmt.Println("File open error")
os.Exit(1)
}
scanner := bufio.NewScanner(f)
for scanner.Scan() {
text := scanner.Text()
deleteBucket(&text)
}
}