Skip to content

Commit d093b28

Browse files
taowei.wtwkkuai
authored andcommitted
support cloud box
1 parent 11adac2 commit d093b28

File tree

4 files changed

+184
-1
lines changed

4 files changed

+184
-1
lines changed

lib/command.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ func (cmd *Command) ossClient(bucket string) (*oss.Client, error) {
306306

307307
localHost, _ := GetString(OptionLocalHost, cmd.options)
308308
bSkipVerifyCert, _ := GetBool(OptionSkipVerfiyCert, cmd.options)
309+
region, _ := GetString(OptionRegion, cmd.options)
310+
signVersion, _ := GetString(OptionSignVersion, cmd.options)
309311

310312
bPassword, _ := GetBool(OptionPassword, cmd.options)
311313

@@ -322,6 +324,13 @@ func (cmd *Command) ossClient(bucket string) (*oss.Client, error) {
322324
}
323325

324326
options := []oss.ClientOption{}
327+
if region != "" {
328+
options = append(options, oss.Region(region))
329+
}
330+
331+
if signVersion != "" {
332+
options = append(options, oss.AuthVersion(oss.AuthVersionType(signVersion)))
333+
}
325334

326335
if strings.EqualFold(mode, "AK") {
327336
if err := cmd.checkCredentials(endpoint, accessKeyID, accessKeySecret); err != nil {
@@ -862,5 +871,6 @@ func GetAllCommands() []interface{} {
862871
&lrbCommand,
863872
&replicationCommand,
864873
&bucketCnameCommand,
874+
&lcbCommand,
865875
}
866876
}

lib/const.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ const (
100100
OptionItem = "item"
101101
OptionUserAgent = "userAgent"
102102
OptionObjectFile = "objectFile"
103+
OptionSignVersion = "signVersion"
104+
OptionRegion = "region"
103105
)
104106

105107
// the elements show in stat object

lib/lcb.go

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package lib
2+
3+
import (
4+
"fmt"
5+
6+
oss "github.com/aliyun/aliyun-oss-go-sdk/oss"
7+
)
8+
9+
var specChineseListCloudBox = SpecText{
10+
11+
synopsisText: "列举云盒信息",
12+
13+
paramText: "[options]",
14+
15+
syntaxText: `
16+
ossutil lcb [-e endpoint]
17+
`,
18+
19+
detailHelpText: `
20+
该命令列举云盒的详细信息
21+
`,
22+
23+
sampleText: `
24+
1) ossutil lcb --sign-version v4 --region cloudbox-id
25+
`,
26+
}
27+
28+
var specEnglishListCloudBox = SpecText{
29+
30+
synopsisText: "List cloud box information",
31+
32+
paramText: "[options]",
33+
34+
syntaxText: `
35+
ossutil lcb [-e endpoint]
36+
`,
37+
38+
detailHelpText: `
39+
This command lists cloud box information
40+
`,
41+
42+
sampleText: `
43+
1) ossutil lcb --sign-version v4 --region cloudbox-id
44+
`,
45+
}
46+
47+
// LcbCommand is the command list region buckets or objects
48+
type LcbCommand struct {
49+
command Command
50+
}
51+
52+
var lcbCommand = LcbCommand{
53+
command: Command{
54+
name: "lcb",
55+
nameAlias: []string{"lcb"},
56+
minArgc: 0,
57+
maxArgc: 1,
58+
specChinese: specChineseListCloudBox,
59+
specEnglish: specEnglishListCloudBox,
60+
group: GroupTypeNormalCommand,
61+
validOptionNames: []string{
62+
OptionConfigFile,
63+
OptionEndpoint,
64+
OptionAccessKeyID,
65+
OptionAccessKeySecret,
66+
OptionSTSToken,
67+
OptionProxyHost,
68+
OptionProxyUser,
69+
OptionProxyPwd,
70+
OptionRetryTimes,
71+
OptionLogLevel,
72+
OptionPassword,
73+
OptionMode,
74+
OptionECSRoleName,
75+
OptionTokenTimeout,
76+
OptionRamRoleArn,
77+
OptionRoleSessionName,
78+
OptionReadTimeout,
79+
OptionConnectTimeout,
80+
OptionSTSRegion,
81+
OptionSkipVerfiyCert,
82+
OptionUserAgent,
83+
OptionRegion,
84+
OptionSignVersion,
85+
OptionLimitedNum,
86+
OptionMarker,
87+
},
88+
},
89+
}
90+
91+
// function for FormatHelper interface
92+
func (lc *LcbCommand) formatHelpForWhole() string {
93+
return lc.command.formatHelpForWhole()
94+
}
95+
96+
func (lc *LcbCommand) formatIndependHelp() string {
97+
return lc.command.formatIndependHelp()
98+
}
99+
100+
// Init simulate inheritance, and polymorphism
101+
func (lc *LcbCommand) Init(args []string, options OptionMapType) error {
102+
return lc.command.Init(args, options, lc)
103+
}
104+
105+
// RunCommand simulate inheritance, and polymorphism
106+
func (lc *LcbCommand) RunCommand() error {
107+
prefix := ""
108+
if len(lc.command.args) > 0 {
109+
cloudURL, err := CloudURLFromString(lc.command.args[0], "")
110+
if err != nil {
111+
return err
112+
}
113+
prefix = cloudURL.bucket
114+
}
115+
116+
limitedNum, _ := GetInt(OptionLimitedNum, lc.command.options)
117+
vmarker, _ := GetString(OptionMarker, lc.command.options)
118+
if vmarker, err := lc.command.getRawMarker(vmarker); err != nil {
119+
return fmt.Errorf("invalid marker: %s, marker is not url encoded, %s", vmarker, err.Error())
120+
}
121+
122+
var num int64
123+
num = 0
124+
125+
client, err := lc.command.ossClient("")
126+
if err != nil {
127+
return err
128+
}
129+
130+
// list all cloudbox
131+
pre := oss.Prefix(prefix)
132+
marker := oss.Marker(vmarker)
133+
for limitedNum < 0 || num < limitedNum {
134+
lcr, err := lc.ossListCloudBoxesRetry(client, pre, marker)
135+
if err != nil {
136+
return err
137+
}
138+
pre = oss.Prefix(lcr.Prefix)
139+
marker = oss.Marker(lcr.NextMarker)
140+
if num == 0 && len(lcr.CloudBoxes) > 0 {
141+
fmt.Printf("%-30s %20s%s%12s%s%s\n", "ID", "Name", "Owner", "Region", "ControlEndpoint", "DataEndpoint")
142+
}
143+
for _, box := range lcr.CloudBoxes {
144+
if limitedNum >= 0 && num >= limitedNum {
145+
break
146+
}
147+
fmt.Printf("%-30s %20s%s%12s%s%s\n", box.Id, box.Name, box.Owner, box.Region, box.ControlEndpoint, box.DataEndpoint)
148+
num++
149+
}
150+
if !lcr.IsTruncated {
151+
break
152+
}
153+
}
154+
return nil
155+
}
156+
157+
func (lc *LcbCommand) ossListCloudBoxesRetry(client *oss.Client, options ...oss.Option) (oss.ListCloudBoxResult, error) {
158+
retryTimes, _ := GetInt(OptionRetryTimes, lc.command.options)
159+
for i := 1; ; i++ {
160+
lbr, err := client.ListCloudBoxes(options...)
161+
if err == nil || int64(i) >= retryTimes {
162+
return lbr, err
163+
}
164+
}
165+
}

lib/option.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,19 @@ var OptionMap = map[string]Option{
261261
"specifies that the oss server's digital certificate file will not be verified"},
262262
OptionItem: Option{"", "--item", "", OptionTypeString, "", "",
263263
"表示命令的功能类型,取值为LOCATION、PROGRESS等",
264-
"specifies the command's function type. the values ​​are LOCATION, PROGRESS, etc"},
264+
"specifies the command's function type. the values are LOCATION, PROGRESS, etc"},
265265
OptionUserAgent: Option{"", "--ua", "", OptionTypeString, "", "",
266266
"指定http请求中的user agent, 会在缺省值后面加上指定值",
267267
"Specify the user agent in the http request, and the specified value will be added after the default value"},
268268
OptionObjectFile: Option{"", "--object-file", "", OptionTypeString, "", "",
269269
"表示所有待处理的objects,取值为一个存在的文件路径",
270270
"Specify all the objects that need to be operated, and the specified value should be a exists file path"},
271+
OptionSignVersion: Option{"", "--sign-version", "", OptionTypeString, "", "",
272+
"http请求使用的签名算法版本, 缺省为空, 表示v1版本",
273+
"The version of the signature algorithm used in the HTTP request. It is empty by default, indicating the V1 version"},
274+
OptionRegion: Option{"", "--region", "", OptionTypeString, "", "",
275+
"bucket所在的地区, 比如cn-hangzhou, 缺省值为空, 如果使用v4签名则必须传入; 如果是云盒场景则为云盒id",
276+
"The region where the bucket is located, such as cn-hangzhou. The default value is empty. If V4 signature is used, it must be inputted; If used in cloud-box, it is the cloud box ID"},
271277
}
272278

273279
func (T *Option) getHelp(language string) string {

0 commit comments

Comments
 (0)