-
Notifications
You must be signed in to change notification settings - Fork 100
/
allpart_size.go
269 lines (222 loc) · 6.48 KB
/
allpart_size.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package lib
import (
"fmt"
"net/url"
"strconv"
oss "github.com/aliyun/aliyun-oss-go-sdk/oss"
)
var specChineseAllPartSize = SpecText{
synopsisText: "获取bucket所有未完成上传的multipart object的分块大小以及总和",
paramText: "bucket_url [options]",
syntaxText: `
ossutil getallpartsize oss://bucket [options]
`,
detailHelpText: `
该命令会获取bucket所有未完成上传的multipart object的分块大小以及总和
用法:
该命令只有一种用法:
1) ossutil getallpartsize oss://bucket [options]
查询bucket的所有未完成上传的multipart object的块大小信息以及总和
`,
sampleText: `
1) 根据bucket查询所有未完成上传的multipart object的块大小信息以及总和
ossutil getallpartsize oss://bucket
`,
}
var specEnglishAllPartSize = SpecText{
synopsisText: "Get bucket all uncompleted multipart objects's parts size and sum size",
paramText: "bucket_url [options]",
syntaxText: `
ossutil getallpartsize oss://bucket [options]
`,
detailHelpText: `
This command will list every uncompleted multipart objects's part size and sum size
Usages:
There is only one usage for this command:
1) ossutil getallpartsize oss://bucket [options]
Get bucket all uncompleted mulitpart objects's parts size and sum size
`,
sampleText: `
1) Get bucket all uncompleted multipart objects's parts size and sum size
ossutil getallpartsize oss://bucket
`,
}
type allPartSizeOptionType struct {
bucketName string
encodingType string
headLineShowed bool
statList []StatPartInfo
}
type AllPartSizeCommand struct {
command Command
apOption allPartSizeOptionType
}
var allPartSizeCommand = AllPartSizeCommand{
command: Command{
name: "getallpartsize",
nameAlias: []string{"getallpartsize"},
minArgc: 1,
maxArgc: 1,
specChinese: specChineseAllPartSize,
specEnglish: specEnglishAllPartSize,
group: GroupTypeNormalCommand,
validOptionNames: []string{
OptionConfigFile,
OptionEndpoint,
OptionAccessKeyID,
OptionAccessKeySecret,
OptionSTSToken,
OptionProxyHost,
OptionProxyUser,
OptionProxyPwd,
OptionEncodingType,
OptionLogLevel,
OptionPassword,
OptionMode,
OptionECSRoleName,
OptionTokenTimeout,
OptionRamRoleArn,
OptionRoleSessionName,
OptionReadTimeout,
OptionConnectTimeout,
OptionSTSRegion,
OptionSkipVerifyCert,
OptionUserAgent,
OptionSignVersion,
OptionRegion,
OptionCloudBoxID,
},
},
}
type StatPartInfo struct {
objectName string
uploadId string
}
// function for FormatHelper interface
func (apc *AllPartSizeCommand) formatHelpForWhole() string {
return apc.command.formatHelpForWhole()
}
func (apc *AllPartSizeCommand) formatIndependHelp() string {
return apc.command.formatIndependHelp()
}
// Init simulate inheritance, and polymorphism
func (apc *AllPartSizeCommand) Init(args []string, options OptionMapType) error {
return apc.command.Init(args, options, apc)
}
// RunCommand simulate inheritance, and polymorphism
func (apc *AllPartSizeCommand) RunCommand() error {
srcBucketUrL, err := GetCloudUrl(apc.command.args[0], "")
if err != nil {
return err
}
apc.apOption.bucketName = srcBucketUrL.bucket
apc.apOption.encodingType, _ = GetString(OptionEncodingType, apc.command.options)
// first:get all object uploadid
err = apc.GetAllStatInfo()
if err != nil {
return err
}
//second:stat every object parts
client, err := apc.command.ossClient(apc.apOption.bucketName)
if err != nil {
return err
}
bucket, err := client.Bucket(apc.apOption.bucketName)
if err != nil {
return err
}
var totalPartCount int64 = 0
var totalPartSize int64 = 0
for _, v := range apc.apOption.statList {
partCount, partSize, err := apc.GetObjectPartsSize(bucket, v)
if err != nil {
return err
}
totalPartCount += partCount
totalPartSize += partSize
}
if totalPartSize > 0 {
fmt.Printf("\ntotal part count:%d\ttotal part size(MB):%.2f\n\n", totalPartCount, float64(totalPartSize/1024)/1024)
}
return nil
}
func (apc *AllPartSizeCommand) GetAllStatInfo() error {
client, err := apc.command.ossClient(apc.apOption.bucketName)
if err != nil {
return err
}
bucket, err := client.Bucket(apc.apOption.bucketName)
if err != nil {
return err
}
keyMarker := ""
uploadIdMarker := ""
for i := 0; ; i++ {
lpOptions := []oss.Option{}
lpOptions = append(lpOptions, oss.MaxParts(1000))
lpOptions = append(lpOptions, oss.KeyMarker(keyMarker))
lpOptions = append(lpOptions, oss.UploadIDMarker(uploadIdMarker))
lpRes, err := bucket.ListMultipartUploads(lpOptions...)
if err != nil {
return err
}
for _, v := range lpRes.Uploads {
var statPartInfo StatPartInfo
statPartInfo.objectName = v.Key
statPartInfo.uploadId = v.UploadID
apc.apOption.statList = append(apc.apOption.statList, statPartInfo)
}
if lpRes.IsTruncated {
keyMarker = lpRes.NextKeyMarker
uploadIdMarker = lpRes.NextUploadIDMarker
} else {
break
}
}
return nil
}
func (apc *AllPartSizeCommand) GetObjectPartsSize(bucket *oss.Bucket, statInfo StatPartInfo) (int64, int64, error) {
var imur oss.InitiateMultipartUploadResult
imur.Bucket = apc.apOption.bucketName
imur.Key = statInfo.objectName
imur.UploadID = statInfo.uploadId
partNumberMarker := 0
var totalPartCount int64 = 0
var totalPartSize int64 = 0
var cloudUrl CloudURL
for i := 0; ; i++ {
lpOptions := []oss.Option{}
lpOptions = append(lpOptions, oss.MaxParts(1000))
lpOptions = append(lpOptions, oss.PartNumberMarker(partNumberMarker))
lpRes, err := bucket.ListUploadedParts(imur, lpOptions...)
if err != nil {
return 0, 0, err
} else {
totalPartCount += int64(len(lpRes.UploadedParts))
if !apc.apOption.headLineShowed && len(lpRes.UploadedParts) > 0 {
fmt.Printf("%-10s\t%-32s\t%-10s\t%s\n", "PartNumber", "UploadId", "Size(Byte)", "Path")
apc.apOption.headLineShowed = true
}
}
for _, v := range lpRes.UploadedParts {
cloudUrl.bucket = apc.apOption.bucketName
if apc.apOption.encodingType == URLEncodingType {
cloudUrl.object = url.QueryEscape(imur.Key)
} else {
cloudUrl.object = imur.Key
}
//PartNumber,uploadId,Size,Path
fmt.Printf("%-10d\t%-32s\t%-10d\t%s\n", v.PartNumber, imur.UploadID, v.Size, cloudUrl.ToString())
totalPartSize += int64(v.Size)
}
if lpRes.IsTruncated {
partNumberMarker, err = strconv.Atoi(lpRes.NextPartNumberMarker)
if err != nil {
return 0, 0, err
}
} else {
break
}
}
return totalPartCount, totalPartSize, nil
}