-
Notifications
You must be signed in to change notification settings - Fork 99
/
stat.go
295 lines (250 loc) · 8.73 KB
/
stat.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package lib
import (
"fmt"
"net/http"
"sort"
"strings"
"time"
oss "github.com/aliyun/aliyun-oss-go-sdk/oss"
)
var specChineseStat = SpecText{
synopsisText: "显示bucket或者object的描述信息",
paramText: "cloud_url [options]",
syntaxText: `
ossutil stat oss://bucket[/object] [--encoding-type url] [--version-id versionId] [--payer requester] [-c file]
`,
detailHelpText: `
该命令获取指定bucket或者object的描述信息。通过set-meta命令设置的object元信息,可以通过
该命令查看。
用法:
该命令有两种用法:
1) ossutil stat oss://bucket [--encoding-type url]
ossutil显示指定bucket的信息,包括创建时间,location,访问的外网域名,内网域名,拥
有者,acl信息。
2) ossutil stat oss://bucket/object [--encoding-type url] [--version-id versionId]
ossutil显示指定object的元信息,包括文件大小,最新更新时间,etag,文件类型,acl,文
件的自定义meta等信息。
`,
sampleText: `
ossutil stat oss://bucket1
ossutil stat oss://bucket1/object
ossutil stat oss://bucket1/object --version-id versionId
ossutil stat oss://bucket1/%e4%b8%ad%e6%96%87 --encoding-type url
ossutil stat oss://bucket1/object --payer requester
`,
}
var specEnglishStat = SpecText{
synopsisText: "Display meta information of bucket or objects",
paramText: "cloud_url [options]",
syntaxText: `
ossutil stat oss://bucket[/object] [--encoding-type url] [--version-id versionId] [--payer requester] [-c file]
`,
detailHelpText: `
The command display the meta information of bucket or objects. The object meta information
setted through set-meta command, can be check by the command.
Usage:
There are three usages:
1) ossutil stat oss://bucket [--encoding-type url]
ossutil display bucket meta info, include creation date, location, extranet endpoint,
intranet endpoint, Owner and acl info.
2) ossutil stat oss://bucket/object [--encoding-type url] [--version-id versionId]
ossutil display object meta info, include file size, last modify time, etag, content-type,
user meta etc.
`,
sampleText: `
ossutil stat oss://bucket1
ossutil stat oss://bucket1/object
ossutil stat oss://bucket1/object --version-id versionId
ossutil stat oss://bucket1/%e4%b8%ad%e6%96%87 --encoding-type url
ossutil stat oss://bucket1/object --payer requester
`,
}
// StatCommand is the command get bucket's or objects' meta information
type StatCommand struct {
command Command
versionId string
commonOptions []oss.Option
}
var statCommand = StatCommand{
command: Command{
name: "stat",
nameAlias: []string{"meta", "info"},
minArgc: 1,
maxArgc: 1,
specChinese: specChineseStat,
specEnglish: specEnglishStat,
group: GroupTypeNormalCommand,
validOptionNames: []string{
OptionEncodingType,
OptionConfigFile,
OptionEndpoint,
OptionAccessKeyID,
OptionAccessKeySecret,
OptionSTSToken,
OptionProxyHost,
OptionProxyUser,
OptionProxyPwd,
OptionRetryTimes,
OptionLogLevel,
OptionVersionId,
OptionRequestPayer,
OptionPassword,
OptionMode,
OptionECSRoleName,
OptionTokenTimeout,
OptionRamRoleArn,
OptionRoleSessionName,
OptionReadTimeout,
OptionConnectTimeout,
OptionSTSRegion,
OptionSkipVerfiyCert,
},
},
}
// function for FormatHelper interface
func (sc *StatCommand) formatHelpForWhole() string {
return sc.command.formatHelpForWhole()
}
func (sc *StatCommand) formatIndependHelp() string {
return sc.command.formatIndependHelp()
}
// Init simulate inheritance, and polymorphism
func (sc *StatCommand) Init(args []string, options OptionMapType) error {
return sc.command.Init(args, options, sc)
}
// RunCommand simulate inheritance, and polymorphism
func (sc *StatCommand) RunCommand() error {
sc.versionId, _ = GetString(OptionVersionId, sc.command.options)
encodingType, _ := GetString(OptionEncodingType, sc.command.options)
cloudURL, err := CloudURLFromString(sc.command.args[0], encodingType)
if err != nil {
return err
}
if cloudURL.bucket == "" {
return fmt.Errorf("invalid cloud url: %s, miss bucket", sc.command.args[0])
}
payer, _ := GetString(OptionRequestPayer, sc.command.options)
if payer != "" {
if payer != strings.ToLower(string(oss.Requester)) {
return fmt.Errorf("invalid request payer: %s, please check", payer)
}
sc.commonOptions = append(sc.commonOptions, oss.RequestPayer(oss.PayerType(payer)))
}
bucket, err := sc.command.ossBucket(cloudURL.bucket)
if err != nil {
return err
}
if cloudURL.object == "" {
return sc.bucketStat(bucket, cloudURL)
}
return sc.objectStat(bucket, cloudURL)
}
func (sc *StatCommand) bucketStat(bucket *oss.Bucket, cloudURL CloudURL) error {
// TODO: go sdk should implement GetBucketInfo
gbar, err := sc.ossGetBucketStatRetry(bucket)
if err != nil {
return err
}
fmt.Printf("%-22s: %s\n", StatName, gbar.BucketInfo.Name)
fmt.Printf("%-22s: %s\n", StatLocation, gbar.BucketInfo.Location)
fmt.Printf("%-22s: %s\n", StatCreationDate, utcToLocalTime(gbar.BucketInfo.CreationDate))
fmt.Printf("%-22s: %s\n", StatExtranetEndpoint, gbar.BucketInfo.ExtranetEndpoint)
fmt.Printf("%-22s: %s\n", StatIntranetEndpoint, gbar.BucketInfo.IntranetEndpoint)
fmt.Printf("%-22s: %s\n", StatACL, gbar.BucketInfo.ACL)
fmt.Printf("%-22s: %s\n", StatOwner, gbar.BucketInfo.Owner.ID)
fmt.Printf("%-22s: %s\n", StatStorageClass, gbar.BucketInfo.StorageClass)
if len(gbar.BucketInfo.RedundancyType) > 0 {
fmt.Printf("%-22s: %s\n", StatRedundancyType, gbar.BucketInfo.RedundancyType)
}
if len(gbar.BucketInfo.SseRule.SSEAlgorithm) > 0 {
fmt.Printf("%-22s: %s\n", StatSSEAlgorithm, gbar.BucketInfo.SseRule.SSEAlgorithm)
}
if len(gbar.BucketInfo.SseRule.KMSMasterKeyID) > 0 {
fmt.Printf("%-22s: %s\n", StatKMSMasterKeyID, gbar.BucketInfo.SseRule.KMSMasterKeyID)
}
if len(gbar.BucketInfo.SseRule.KMSDataEncryption) > 0 {
fmt.Printf("%-22s: %s\n", StatKMSDataEncryption, gbar.BucketInfo.SseRule.KMSDataEncryption)
}
fmt.Printf("%-22s: %s\n", StatTransferAcceleration, gbar.BucketInfo.TransferAcceleration)
fmt.Printf("%-22s: %s\n", StatCrossRegionReplication, gbar.BucketInfo.CrossRegionReplication)
return nil
}
func (sc *StatCommand) ossGetBucketStatRetry(bucket *oss.Bucket) (oss.GetBucketInfoResult, error) {
retryTimes, _ := GetInt(OptionRetryTimes, sc.command.options)
for i := 1; ; i++ {
gbar, err := bucket.Client.GetBucketInfo(bucket.BucketName, sc.commonOptions...)
if err == nil {
return gbar, err
}
if int64(i) >= retryTimes {
return gbar, BucketError{err, bucket.BucketName}
}
}
}
func (sc *StatCommand) objectStat(bucket *oss.Bucket, cloudURL CloudURL) error {
// acl info
goar, err := sc.ossGetObjectACLRetry(bucket, cloudURL.object)
if err != nil {
return err
}
// normal info
statOptions := []oss.Option{}
if len(sc.versionId) > 0 {
statOptions = append(statOptions, oss.VersionId(sc.versionId))
}
statOptions = append(statOptions, sc.commonOptions...)
props, err := sc.command.ossGetObjectStatRetry(bucket, cloudURL.object, statOptions...)
if err != nil {
return err
}
sortNames := []string{}
attrMap := map[string]string{}
maxNameLen := 0
for name := range props {
if len(name) > maxNameLen {
maxNameLen = len(name)
}
ln := strings.ToLower(name)
if ln != strings.ToLower(oss.HTTPHeaderDate) &&
ln != strings.ToLower(oss.HTTPHeaderOssRequestID) &&
ln != strings.ToLower(oss.HTTPHeaderServer) &&
ln != "x-oss-server-time" &&
ln != "connection" {
sortNames = append(sortNames, name)
attrMap[name] = props.Get(name)
}
}
sortNames = append(sortNames, "Owner")
sortNames = append(sortNames, "ACL")
attrMap[StatOwner] = goar.Owner.ID
attrMap[StatACL] = goar.ACL
if lm, err := time.Parse(http.TimeFormat, attrMap[StatLastModified]); err == nil {
attrMap[StatLastModified] = fmt.Sprintf("%s", utcToLocalTime(lm.UTC()))
}
sort.Strings(sortNames)
for _, name := range sortNames {
if strings.ToLower(name) != "etag" {
fmt.Printf("%-[1]*s: %s\n", maxNameLen+2, name, attrMap[name])
} else {
fmt.Printf("%-[1]*s: %s\n", maxNameLen+2, name, strings.Trim(attrMap[name], "\""))
}
}
return nil
}
func (sc *StatCommand) ossGetObjectACLRetry(bucket *oss.Bucket, object string) (oss.GetObjectACLResult, error) {
retryTimes, _ := GetInt(OptionRetryTimes, sc.command.options)
aclOptions := []oss.Option{}
if len(sc.versionId) > 0 {
aclOptions = append(aclOptions, oss.VersionId(sc.versionId))
}
aclOptions = append(aclOptions, sc.commonOptions...)
for i := 1; ; i++ {
goar, err := bucket.GetObjectACL(object, aclOptions...)
if err == nil {
return goar, err
}
if int64(i) >= retryTimes {
return goar, ObjectError{err, bucket.BucketName, object}
}
}
}