/
main.go
239 lines (205 loc) · 5.83 KB
/
main.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"context"
"flag"
"fmt"
"hash/crc32"
"log"
"os"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
//windows: go build -v --tags netgo -ldflags "-s -w -extldflags \"-static\"" -o s3cleaner.exe main.go
//win-linux: set GOOS=linux set GOARCH=amd64 go build -v --tags netgo -ldflags "-s -w -extldflags \"-static\"" -o s3cleaner main.go
//linux: GOOS=linux GOARCH=amd64 go build -v --tags netgo -ldflags '-s -w -extldflags "-static"' -o s3cleaner main.go
var (
s3AK = os.Getenv("S3_AK")
s3SK = os.Getenv("S3_SK")
s3Endpoint = os.Getenv("S3_ENDPOINT")
s3Bucket = os.Getenv("S3_BUCKET")
s3Region = os.Getenv("S3_REGION")
s3CopyBucket = os.Getenv("S3_COPY_BUCKET")
)
var (
logger *log.Logger
s3client *s3.S3
timeOrig, _ = time.Parse("20060102", "20161212")
)
var mode = flag.Int("mode", 1, "mode1 scan, mode2 moveobject mode3 resotreobject")
var pageLen = flag.Int("page", 1, "scan max page len")
var expday = flag.Int("expday", 365*4, "day of expire")
var sample = flag.Int("sample", 1, "if 1 then sample")
var hashn = flag.Uint("hashn", 10, "hash num")
var hashi = flag.Uint("hashi", 0, "hash index")
func main() {
flag.Parse()
ctx := context.Background()
fmt.Println(fmt.Sprintf("options: mode: %v, page: %v, expireday: %v, sample: %v, hashn: %v, hashi: %v",
*mode, *pageLen, *expday, *sample, *hashn, *hashi))
s3client = initS3Client()
initSample()
var matchFileCount, matchfilesize int64
var total, totalSize int64
if *mode == 1 {
//statiscs
total, totalSize = scan(ctx, s3Bucket, *sample, scanMatcher, func(obj *s3.Object) bool {
matchFileCount++
matchfilesize += *obj.Size
return true
})
} else if *mode == 2 {
//move to bucket tmp
total, totalSize = scan(ctx, s3Bucket, *sample, scanMatcher, func(obj *s3.Object) bool {
matchFileCount++
matchfilesize += *obj.Size
objName := strings.ToLower(*obj.Key)
crc32 := uint(crc32.ChecksumIEEE([]byte(objName)))
if crc32%(*hashn) != (*hashi) {
return true
}
if !strings.HasSuffix(objName, ".mp4") {
_, err := s3client.CopyObject(&s3.CopyObjectInput{
CopySource: aws.String(fmt.Sprintf("%s/%s", s3Bucket, *obj.Key)),
Bucket: aws.String(s3CopyBucket),
Key: obj.Key,
})
if err != nil {
logger.Printf("CopyObject: %v, err: %v\n", *obj.Key, err)
return true //skip the file
}
}
_, err := s3client.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(s3Bucket),
Key: obj.Key,
})
if err != nil {
logger.Fatalf("DeleteObject err: %v\n", err)
}
return true
})
} else if *mode == 3 {
//restore
total, totalSize = scan(ctx, s3CopyBucket, *sample, func(*s3.Object) bool {
return true
}, func(obj *s3.Object) bool {
matchFileCount++
matchfilesize += *obj.Size
_, err := s3client.CopyObject(&s3.CopyObjectInput{
CopySource: aws.String(fmt.Sprintf("%s/%s", s3CopyBucket, *obj.Key)),
Bucket: aws.String(s3Bucket),
Key: obj.Key,
})
if err != nil {
logger.Fatalf("Restore err: %v\n", err)
}
_, err = s3client.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(s3CopyBucket),
Key: obj.Key,
})
if err != nil {
logger.Fatalf("DeleteTmpObject err: %v\n", err)
}
return true
})
}
reportStr := fmt.Sprintf("匹配文件: %v(%vGB), 总文件数: %v(%vGB)", matchFileCount, matchfilesize/1024/1024/1024,
total, totalSize/1024/1024/1024)
fmt.Println(reportStr)
}
func scanMatcher(obj *s3.Object) bool {
keyName := *obj.Key
if strings.HasSuffix(keyName, ".ts") {
if len(keyName) > 73 {
timestr := keyName[65:73]
//fmt.Println(timestr)
t, err := time.Parse("20060102", timestr)
if err != nil {
return false
}
if t.Before(time.Now().AddDate(0, 0, -*expday)) && t.After(timeOrig) {
//fmt.Println(keyName)
return true
}
}
} else if strings.HasSuffix(keyName, ".mp4") {
if len(keyName) > 43 {
timestr := keyName[33:43] //13
ts, err := strconv.ParseInt(timestr, 10, 64)
if err != nil {
return false
}
t := time.Unix(ts, 0)
if t.Before(time.Now().AddDate(0, 0, -*expday)) && t.After(timeOrig) {
//fmt.Println(keyName)
return true
}
}
}
return false
}
func scan(ctx context.Context, srcBucket string, sample int, matcher, handler func(*s3.Object) bool) (int64, int64) {
total := int64(0)
totalSize := int64(0)
i := 0
matchIndex := int64(0)
params := &s3.ListObjectsInput{Bucket: aws.String(srcBucket)}
err := s3client.ListObjectsPagesWithContext(ctx, params, func(output *s3.ListObjectsOutput, end bool) bool {
for _, obj := range output.Contents {
if matcher(obj) {
if sample == 1 {
sampleOutput(obj, matchIndex)
matchIndex++
}
handler(obj)
}
total++
totalSize += *obj.Size
}
if end {
return false
}
i++
return i < *pageLen
})
if err != nil {
fmt.Fprintf(os.Stderr, "ListObjectsPagesWithContext err: %v\n", err)
}
return total, totalSize
}
func initS3Client() *s3.S3 {
sess, _ := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(s3AK, s3SK, ""),
Region: aws.String(s3Region),
Endpoint: aws.String(s3Endpoint),
DisableSSL: aws.Bool(true),
S3ForcePathStyle: aws.Bool(true),
},
)
return s3.New(sess)
}
func sampleOutput(obj *s3.Object, index int64) {
hashBase := uint32(*pageLen) / 1000
if hashBase < 2 {
hashBase = 2
}
keyName := *obj.Key
if uint32(index)%hashBase == 1 {
logger.Println(keyName)
}
}
func initSample() {
if *sample == 1 {
fPath := "sample_" + time.Now().Format("2006-01-02_15_04_05.txt")
file, err := os.Create(fPath)
if err != nil {
log.Fatalln("fail to create log file", err)
return
}
logger = log.New(file, "", log.LstdFlags)
}
}