describe_zones.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
  6. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
  7. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/profile"
  8. cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312"
  9. )
  10. func main() {
  11. // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
  12. // 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值
  13. credential := common.NewCredential(
  14. os.Getenv("TENCENTCLOUD_SECRET_ID"),
  15. os.Getenv("TENCENTCLOUD_SECRET_KEY"),
  16. )
  17. // 实例化一个客户端配置对象,可以指定超时时间等配置
  18. cpf := profile.NewClientProfile()
  19. cpf.HttpProfile.ReqMethod = "GET"
  20. cpf.HttpProfile.ReqTimeout = 5
  21. cpf.Debug = true
  22. // 实例化要请求产品(以cvm为例)的client对象
  23. client, _ := cvm.NewClient(credential, "ap-beijing", cpf)
  24. // 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
  25. request := cvm.NewDescribeZonesRequest()
  26. // 通过client对象调用想要访问的接口,需要传入请求对象
  27. response, err := client.DescribeZones(request)
  28. // 处理异常
  29. if _, ok := err.(*errors.TencentCloudSDKError); ok {
  30. fmt.Printf("An API error has returned: %s", err)
  31. return
  32. }
  33. // unexpected errors
  34. if err != nil {
  35. panic(err)
  36. }
  37. // 打印返回的json字符串
  38. fmt.Printf("%s", response.ToJsonString())
  39. }