GithubHelp home page GithubHelp logo

Comments (4)

MrSong0607 avatar MrSong0607 commented on July 19, 2024

官网上面的示例只给出了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.

solarhell avatar solarhell commented on July 19, 2024

注意看commit
f845dc5

from go-cos.

mozillazg avatar mozillazg commented on July 19, 2024

@MrSong0607 感谢分享。还有一种方法是使用 @solarhell 提到的预签名 URL 来上传文件 #5 你可以看看是否对你有帮助。

等我有空的时候会修改程序支持使用临时密钥,也欢迎提交 PR(倾向于只增加支持使用临时密钥计算签名的功能,获取临时密钥的操作由独立的包来实现,go-cos 不实现获取临时密钥的功能 )。

from go-cos.

MrSong0607 avatar MrSong0607 commented on July 19, 2024

@solarhell @mozillazg
了解,感谢两位

from go-cos.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.