You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.6 KiB

  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/QcloudApi/qcloud_sign_golang"
  7. "github.com/tencentyun/cos-go-sdk-v5"
  8. "github.com/tencentyun/cos-go-sdk-v5/debug"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. )
  13. // Use Qcloud api github.com/QcloudApi/qcloud_sign_golang
  14. // doc https://cloud.tencent.com/document/product/436/14048
  15. type Credent struct {
  16. SessionToken string `json:"sessionToken"`
  17. TmpSecretID string `json:"tmpSecretId"`
  18. TmpSecretKey string `json:"tmpSecretKey"`
  19. }
  20. // Data data in sts response body
  21. type Data struct {
  22. Credentials Credent `json:"credentials"`
  23. }
  24. // Response sts response body
  25. // In qcloud_sign_golang this response only return ak, sk and token
  26. type Response struct {
  27. Dat Data `json:"data"`
  28. }
  29. func main() {
  30. // 替换实际的 SecretId 和 SecretKey
  31. secretID := "ak"
  32. secretKey := "sk"
  33. // 配置
  34. config := map[string]interface{}{"secretId": secretID, "secretKey": secretKey, "debug": false}
  35. // 请求参数
  36. params := map[string]interface{}{"Region": "gz", "Action": "GetFederationToken", "name": "alantong", "policy": "{\"statement\": [{\"action\": [\"name/cos:GetObject\",\"name/cos:PutObject\"],\"effect\": \"allow\",\"resource\":[\"qcs::cos:ap-guangzhou:uid/1253960454:prefix//1253960454/alangz/*\"]}],\"version\": \"2.0\"}"}
  37. // 发送请求
  38. retData, err := QcloudApi.SendRequest("sts", params, config)
  39. if err != nil {
  40. fmt.Print("Error.", err)
  41. return
  42. }
  43. r := &Response{}
  44. err = json.Unmarshal([]byte(retData), r)
  45. if err != nil {
  46. fmt.Println(err)
  47. return
  48. }
  49. //获取临时ak、sk、token
  50. tAk := r.Dat.Credentials.TmpSecretID
  51. tSk := r.Dat.Credentials.TmpSecretKey
  52. token := r.Dat.Credentials.SessionToken
  53. u, _ := url.Parse("https://alangz-1253960454.cos.ap-guangzhou.myqcloud.com")
  54. b := &cos.BaseURL{BucketURL: u}
  55. c := cos.NewClient(b, &http.Client{
  56. Transport: &cos.AuthorizationTransport{
  57. SecretID: tAk,
  58. SecretKey: tSk,
  59. SessionToken: token,
  60. Transport: &debug.DebugRequestTransport{
  61. RequestHeader: true,
  62. RequestBody: true,
  63. ResponseHeader: true,
  64. ResponseBody: true,
  65. },
  66. },
  67. })
  68. name := "test/objectPut.go"
  69. f := strings.NewReader("test")
  70. _, err = c.Object.Put(context.Background(), name, f, nil)
  71. if err != nil {
  72. panic(err)
  73. }
  74. name = "test/put_option.go"
  75. f = strings.NewReader("test xxx")
  76. opt := &cos.ObjectPutOptions{
  77. ObjectPutHeaderOptions: &cos.ObjectPutHeaderOptions{
  78. ContentType: "text/html",
  79. },
  80. ACLHeaderOptions: &cos.ACLHeaderOptions{
  81. //XCosACL: "public-read",
  82. XCosACL: "private",
  83. },
  84. }
  85. _, err = c.Object.Put(context.Background(), name, f, opt)
  86. if err != nil {
  87. panic(err)
  88. }
  89. }