-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (57 loc) · 1.38 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
package postobj
import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/json"
)
type PostObject struct {
accessKeyId string
accessKeySecret string
policy *Policy
}
// UploadParams 直传OSS的参数
func (helper *PostObject) UploadParams() (res UploadParams) {
res = UploadParams{
AccessKeyId: helper.accessKeyId,
Signature: helper.signature(),
}
if helper.policy != nil {
res.Policy = helper.policyBase64()
}
return
}
// With 用于设置Policy
func (helper *PostObject) With(p Policy) *PostObject {
return &PostObject{
accessKeyId: helper.accessKeyId,
accessKeySecret: helper.accessKeySecret,
policy: &p,
}
}
func (helper *PostObject) policyBase64() string {
if helper.policy == nil {
return ""
}
policyTxt, err := json.Marshal(helper.policy)
if err != nil {
panic(err)
}
return base64.StdEncoding.EncodeToString(policyTxt)
}
func (helper *PostObject) signature() string {
mac := hmac.New(sha1.New, []byte(helper.accessKeySecret))
mac.Write([]byte(helper.policyBase64()))
return base64.StdEncoding.EncodeToString(mac.Sum(nil))
}
func New(accessKeyId, accessKeySecret string) *PostObject {
return &PostObject{
accessKeyId: accessKeyId,
accessKeySecret: accessKeySecret,
}
}
type UploadParams struct {
AccessKeyId string `json:"accessKeyId"`
Policy string `json:"policy"`
Signature string `json:"signature"`
}