Go SDK使用范例
以同步执行函数为例,提供封装后的SDK。为了保证您成功使用以下示例代码,您需要先添加对应语言的SDK依赖。
SDK信息 |
说明 |
---|---|
安装 |
go get -u github.com/huaweicloud/huaweicloud-sdk-go-v3 |
相关链接 |
SDK的请求参数、响应参数、请求示例和响应示例与对应的API一致,具体参数和示例说明请参见同步执行函数接口。
SDK调用示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := functiongraph.NewFunctionGraphClient( functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.InvokeFunctionRequest{} var listInvokeFunctionRequestBodybody = map[string]interface{}{ "k": "v", } request.Body = listInvokeFunctionRequestBodybody response, err := client.InvokeFunction(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } } |

AK/SK、region(EndPoint)、project_id(项目ID)的获取请参见AK/SK签名认证操作指导。
func_name、version在函数界面上获取。
xCFFRequestVersionRequest:返回体格式,取值v0,v1。v0:默认返回文本格式;v1:默认返回json格式,sdk需要使用此值。