-
Notifications
You must be signed in to change notification settings - Fork 100
/
signurl.go
160 lines (123 loc) · 4.35 KB
/
signurl.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
package lib
import (
"fmt"
oss "github.com/aliyun/aliyun-oss-go-sdk/oss"
)
var specChineseSignurl = SpecText{
synopsisText: "生成object下载链接",
paramText: "cloud_url [meta] [options]",
syntaxText: `
ossutil sign cloud_url [--timeout t] [--version-id versionId]
`,
detailHelpText: `
该命令签名用户指定的cloud_url,生成经过签名的url可供第三方用户访问object,其中cloud_url
必须为形如:oss://bucket/object的cloud_url,bucket和object不可缺少。通过--timeout选项指
定url的过期时间,默认为60s。通过--version-id选项指定版本号。
用法:
ossutil sign oss://bucket/object [--timeout t] [--version-id versionId]
`,
sampleText: `
ossutil sign oss://bucket1/object1
生成oss://bucket1/object1的签名url,超时时间60s
ossutil sign oss://bucket1/object1 --timeout 300
生成oss://bucket1/object1的签名url,超时时间300s
ossutil sign oss://tempb1/test%20a%2Bb' --encoding-type url
生成oss://tempb1/'test a+b'的签名url,超时时间60s
ossutil sign oss://bucket1/object1 --version-id versionId
生成指定版本的 oss://bucket1/object1的签名url,超时时间60s
`,
}
var specEnglishSignurl = SpecText{
synopsisText: "Generate download link for object",
paramText: "cloud_url [options]",
syntaxText: `
ossutil sign cloud_url [--timeout t] [--version-id versionId]
`,
detailHelpText: `
The command will generate signature for user specified cloud_url. This signed url can
be used by third-party to access the object.
Where, cloud_url must like: oss://bucket/object
Use --timeout to specify the expire time of url, the default is 60s.
Use --version-id to specify the version.
Usage:
ossutil sign oss://bucket/object [--timeout t] [--version-id versionId]
`,
sampleText: `
ossutil sign oss://bucket1/object1
Generate the signature of oss://bucket1/object1 with expire time 60s
ossutil sign oss://bucket1/object1 --timeout 300
Generate the signature of oss://bucket1/object1 with expire time 300s
ossutil sign oss://tempb1/test%20a%2Bb' --encoding-type url
Generate the signature of oss://tempb1/'test a+b' with expire time 60s
ossutil sign oss://bucket1/object1 --version-id versionId
Generate the signature of a specific version of oss://bucket1/object1 with expire time 60s
`,
}
// SignurlCommand definition
type SignurlCommand struct {
command Command
}
var signURLCommand = SignurlCommand{
command: Command{
name: "sign",
nameAlias: []string{},
minArgc: 1,
maxArgc: 1,
specChinese: specChineseSignurl,
specEnglish: specEnglishSignurl,
group: GroupTypeNormalCommand,
validOptionNames: []string{
OptionTimeout,
OptionEncodingType,
OptionConfigFile,
OptionEndpoint,
OptionAccessKeyID,
OptionAccessKeySecret,
OptionSTSToken,
OptionLogLevel,
OptionVersionId,
},
},
}
// function for FormatHelper interface
func (sc *SignurlCommand) formatHelpForWhole() string {
return sc.command.formatHelpForWhole()
}
func (sc *SignurlCommand) formatIndependHelp() string {
return sc.command.formatIndependHelp()
}
// Init simulate inheritance, and polymorphism
func (sc *SignurlCommand) Init(args []string, options OptionMapType) error {
return sc.command.Init(args, options, sc)
}
// RunCommand simulate inheritance, and polymorphism
func (sc *SignurlCommand) RunCommand() error {
encodingType, _ := GetString(OptionEncodingType, sc.command.options)
cloudURL, err := ObjectURLFromString(sc.command.args[0], encodingType)
if err != nil {
return err
}
timeout, _ := GetInt(OptionTimeout, sc.command.options)
versionId, _ := GetString(OptionVersionId, sc.command.options)
bucket, err := sc.command.ossBucket(cloudURL.bucket)
if err != nil {
return err
}
var options []oss.Option
if len(versionId) > 0 {
options = append(options, oss.VersionId(versionId))
}
str, err := sc.ossSign(bucket, cloudURL.object, timeout, options...)
if err != nil {
return err
}
fmt.Println(str)
return nil
}
func (sc *SignurlCommand) ossSign(bucket *oss.Bucket, object string, timeout int64, options ...oss.Option) (string, error) {
str, err := bucket.SignURL(object, oss.HTTPMethod(DefaultMethod), timeout, options...)
if err == nil {
return str, nil
}
return str, ObjectError{err, bucket.BucketName, object}
}