Go SDK使用范例
以同步执行函数为例,提供封装后的SDK。为了保证您成功使用以下示例代码,您需要先添加对应语言的SDK依赖。
SDK信息 |
说明 |
---|---|
安装 |
go get -u github.com/huaweicloud/huaweicloud-sdk-go-v3 |
相关链接 |
SDK的请求参数、响应参数、请求示例和响应示例与对应的API一致,具体参数和示例说明请参见同步执行函数接口。
SDK调用示例
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() { // 该示例仅为测试使用,在生产环境中请勿硬编码ak sk 信息在代码中 ak := "<YOUR AK>" sk := "<YOUR SK>" securityToken := os.Getenv("HUAWEICLOUD_SDK_SECURITY_TOKEN") projectId := "{your projectId string}" auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). WithSecurityToken(securityToken). WithProjectId(projectId). SafeBuild() client := functiongraph.NewFunctionGraphClient( functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<region>")). WithCredential(auth). Build()) request := &model.InvokeFunctionRequest{} xCffLogTypeRequest:= "tail" request.XCffLogType = &xCffLogTypeRequest xCFFRequestVersionRequest:= "v1" request.XCFFRequestVersion = &xCFFRequestVersionRequest request.FunctionUrn = "urn:fss:<region>:<project_id>:function:default:<func_name>:<version>" 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需要使用此值。