1
|
package oss
|
1
|
package oss
|
2
|
|
2
|
|
|
|
3
|
+import (
|
|
|
4
|
+ "crypto/hmac"
|
|
|
5
|
+ "crypto/sha1"
|
|
|
6
|
+ "encoding/base64"
|
|
|
7
|
+ "encoding/json"
|
|
|
8
|
+ "fmt"
|
|
|
9
|
+ "hash"
|
|
|
10
|
+ "io"
|
|
|
11
|
+ "time"
|
|
|
12
|
+)
|
|
|
13
|
+
|
3
|
type PolicyToken struct {
|
14
|
type PolicyToken struct {
|
4
|
AccessKeyId string `json:"accessid"`
|
15
|
AccessKeyId string `json:"accessid"`
|
5
|
Host string `json:"host"`
|
16
|
Host string `json:"host"`
|
|
@@ -10,54 +21,63 @@ type PolicyToken struct { |
|
@@ -10,54 +21,63 @@ type PolicyToken struct { |
10
|
Callback string `json:"callback"`
|
21
|
Callback string `json:"callback"`
|
11
|
}
|
22
|
}
|
12
|
|
23
|
|
|
|
24
|
+type PolicyConfigStruct struct {
|
|
|
25
|
+ Expiration string `json:"expiration"`
|
|
|
26
|
+ Conditions [][]string `json:"conditions"`
|
|
|
27
|
+}
|
|
|
28
|
+
|
13
|
type CallbackParam struct {
|
29
|
type CallbackParam struct {
|
14
|
CallbackUrl string `json:"callbackUrl"`
|
30
|
CallbackUrl string `json:"callbackUrl"`
|
15
|
CallbackBody string `json:"callbackBody"`
|
31
|
CallbackBody string `json:"callbackBody"`
|
16
|
CallbackBodyType string `json:"callbackBodyType"`
|
32
|
CallbackBodyType string `json:"callbackBodyType"`
|
17
|
}
|
33
|
}
|
18
|
|
34
|
|
19
|
-// func get_policy_token() string {
|
|
|
20
|
-// now := time.Now().Unix()
|
|
|
21
|
-// expire_end := now + expire_time
|
|
|
22
|
-// var tokenExpire = get_gmt_iso8601(expire_end)
|
35
|
+var (
|
|
|
36
|
+ // 用户上传文件时指定的文件夹。
|
|
|
37
|
+ upload_dir = "demo/"
|
|
|
38
|
+)
|
23
|
|
39
|
|
24
|
-// //create post policy json
|
|
|
25
|
-// var config ConfigStruct
|
|
|
26
|
-// config.Expiration = tokenExpire
|
|
|
27
|
-// var condition []string
|
|
|
28
|
-// condition = append(condition, "starts-with")
|
|
|
29
|
-// condition = append(condition, "$key")
|
|
|
30
|
-// condition = append(condition, upload_dir)
|
|
|
31
|
-// config.Conditions = append(config.Conditions, condition)
|
40
|
+func GetPolicyToken() string {
|
|
|
41
|
+ now := time.Now().Unix()
|
|
|
42
|
+ expire_end := now + 30
|
|
|
43
|
+ var tokenExpire = time.Unix(expire_end, 0).Format("2006-01-02T15:04:05Z")
|
32
|
|
44
|
|
33
|
-// //calucate signature
|
|
|
34
|
-// result,err:=json.Marshal(config)
|
|
|
35
|
-// debyte := base64.StdEncoding.EncodeToString(result)
|
|
|
36
|
-// h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(accessKeySecret))
|
|
|
37
|
-// io.WriteString(h, debyte)
|
|
|
38
|
-// signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
|
|
|
39
|
-
|
|
|
40
|
-// var callbackParam CallbackParam
|
|
|
41
|
-// callbackParam.CallbackUrl = callbackUrl
|
|
|
42
|
-// callbackParam.CallbackBody = "filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}"
|
|
|
43
|
-// callbackParam.CallbackBodyType = "application/x-www-form-urlencoded"
|
|
|
44
|
-// callback_str,err:=json.Marshal(callbackParam)
|
|
|
45
|
-// if err != nil {
|
|
|
46
|
-// fmt.Println("callback json err:", err)
|
|
|
47
|
-// }
|
|
|
48
|
-// callbackBase64 := base64.StdEncoding.EncodeToString(callback_str)
|
|
|
49
|
-
|
|
|
50
|
-// var policyToken PolicyToken
|
|
|
51
|
-// policyToken.AccessKeyId = accessKeyId
|
|
|
52
|
-// policyToken.Host = host
|
|
|
53
|
-// policyToken.Expire = expire_end
|
|
|
54
|
-// policyToken.Signature = string(signedStr)
|
|
|
55
|
-// policyToken.Directory = upload_dir
|
|
|
56
|
-// policyToken.Policy = string(debyte)
|
|
|
57
|
-// policyToken.Callback = string(callbackBase64)
|
|
|
58
|
-// response,err:=json.Marshal(policyToken)
|
|
|
59
|
-// if err != nil {
|
|
|
60
|
-// fmt.Println("json err:", err)
|
|
|
61
|
-// }
|
|
|
62
|
-// return string(response)
|
|
|
63
|
-// } |
45
|
+ //create post policy json
|
|
|
46
|
+ var policyConfig PolicyConfigStruct
|
|
|
47
|
+ policyConfig.Expiration = tokenExpire
|
|
|
48
|
+ var condition []string
|
|
|
49
|
+ condition = append(condition, "starts-with")
|
|
|
50
|
+ condition = append(condition, "$key")
|
|
|
51
|
+ condition = append(condition, upload_dir)
|
|
|
52
|
+ policyConfig.Conditions = append(policyConfig.Conditions, condition)
|
|
|
53
|
+ ossConfig := NewOssConfig()
|
|
|
54
|
+ //calucate signature
|
|
|
55
|
+ result, err := json.Marshal(policyConfig)
|
|
|
56
|
+ debyte := base64.StdEncoding.EncodeToString(result)
|
|
|
57
|
+ h := hmac.New(func() hash.Hash { return sha1.New() }, []byte(ossConfig.accessKey))
|
|
|
58
|
+ io.WriteString(h, debyte)
|
|
|
59
|
+ signedStr := base64.StdEncoding.EncodeToString(h.Sum(nil))
|
|
|
60
|
+ var callbackParam CallbackParam
|
|
|
61
|
+ callbackParam.CallbackUrl = ossConfig.callbackUrl
|
|
|
62
|
+ callbackParam.CallbackBody = "filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}"
|
|
|
63
|
+ callbackParam.CallbackBodyType = "application/x-www-form-urlencoded"
|
|
|
64
|
+ callback_str, err := json.Marshal(callbackParam)
|
|
|
65
|
+ if err != nil {
|
|
|
66
|
+ fmt.Println("callback json err:", err)
|
|
|
67
|
+ }
|
|
|
68
|
+ callbackBase64 := base64.StdEncoding.EncodeToString(callback_str)
|
|
|
69
|
+ var policyToken PolicyToken
|
|
|
70
|
+ policyToken.AccessKeyId = ossConfig.accessID
|
|
|
71
|
+ policyToken.Host = ossConfig.hostStr
|
|
|
72
|
+ policyToken.Expire = expire_end
|
|
|
73
|
+ policyToken.Signature = string(signedStr)
|
|
|
74
|
+ //TODO 文件夹另外处理
|
|
|
75
|
+ policyToken.Directory = upload_dir
|
|
|
76
|
+ policyToken.Policy = string(debyte)
|
|
|
77
|
+ policyToken.Callback = string(callbackBase64)
|
|
|
78
|
+ response, err := json.Marshal(policyToken)
|
|
|
79
|
+ if err != nil {
|
|
|
80
|
+ fmt.Println("json err:", err)
|
|
|
81
|
+ }
|
|
|
82
|
+ return string(response)
|
|
|
83
|
+} |