Comments (4)
官网上面的示例只给出了python版,我自己写了个golang版,希望能整合进去
//AppTempSession .
func AppTempSession(secretID, secretKey string) (*QCloudTokenResult, error) {
policy := `{"statement":[{"action":["name/cos:*"],"effect":"allow","resource":"*"}],"version":"2.0"}`
baseURL, err := url.Parse("https://sts.api.qcloud.com/v2/index.php")
if err != nil {
return nil, err
}
param := make(map[string]string)
param["name"] = "someone"
param["policy"] = policy //base64.StdEncoding.EncodeToString([]byte(policy))
param["durationSeconds"] = "7200"
param["Timestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
rand.Seed(time.Now().Unix())
param["Nonce"] = strconv.Itoa(rand.Int())
param["SecretId"] = secretID
param["Action"] = "GetFederationToken"
sign := genSignature("get", baseURL.Host+baseURL.Path, secretKey, param)
param["Signature"] = sign
uParams := url.Values{}
for k := range param {
uParams.Add(k, param[k])
}
finalURL := baseURL.String() + "?" + uParams.Encode()
resp, err := http.Get(finalURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
bd, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
tk := &QCloudTokenResult{}
return tk, JSONUnMarshal(string(bd), tk)
}
func genSignature(method, uri, secretkey string, param map[string]string) string {
param["SignatureMethod"] = "HmacSHA1"
var keys []string
for k := range param {
keys = append(keys, k)
}
sort.Strings(keys)
str := strings.ToUpper(method) + uri + "?"
for index := range keys {
if index > 0 {
str = str + "&"
}
str = str + keys[index] + "=" + param[keys[index]]
}
h := hmac.New(sha1.New, []byte(secretkey))
if _, err := h.Write([]byte(str)); err != nil {
return ""
}
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
from go-cos.
注意看commit
f845dc5
from go-cos.
@MrSong0607 感谢分享。还有一种方法是使用 @solarhell 提到的预签名 URL 来上传文件 #5 你可以看看是否对你有帮助。
等我有空的时候会修改程序支持使用临时密钥,也欢迎提交 PR(倾向于只增加支持使用临时密钥计算签名的功能,获取临时密钥的操作由独立的包来实现,go-cos
不实现获取临时密钥的功能 )。
from go-cos.
@solarhell @mozillazg
了解,感谢两位
from go-cos.
Related Issues (9)
- Is there any way to point the specific ip and port for client? HOT 4
- Object 相关 API 支持设置 version id
- 上传失败 unsupported protocol scheme "”
- 临时密钥的过期时间如何设置? HOT 2
- Message: The Signature you specified is invalid. HOT 6
- not close body seems lead to leak HOT 2
- 内网的时候x509有办法吗 HOT 11
- 上传文件不需要提供 Content-Length 疑问 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-cos.