Java SDK使用范例
以同步执行函数为例,提供封装后的SDK。为了保证您成功使用以下示例代码,您需要先添加对应语言的SDK依赖。
SDK信息 |
说明 |
---|---|
安装 |
<dependency> <groupId>com.huaweicloud.sdk</groupId> <artifactId>huaweicloud-sdk-functiongraph</artifactId> <version>${version}</version> </dependency> |
相关链接 |
“${version}”是SDK的版本号,请根据实际填写。
SDK的请求参数、响应参数、请求示例和响应示例与对应的API一致,具体参数和示例说明请参见同步执行函数接口。
SDK调用示例
package com.huaweicloud.sdk.test; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.functiongraph.v2.region.FunctionGraphRegion; import com.huaweicloud.sdk.functiongraph.v2.*; import com.huaweicloud.sdk.functiongraph.v2.model.*; public class InvokeFunctionSolution { public static void main(String[] args) { // 该示例仅为测试使用,在生产环境中请勿硬编码ak sk 信息在代码中 String ak = "<YOUR AK>"; String sk = "<YOUR SK>"; String securityToken = System.getenv("HUAWEICLOUD_SDK_SECURITY_TOKEN"); String projectId = "{your projectId string}"; ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk) .withSecurityToken(securityToken) .withProjectId(projectId) FunctionGraphClient client = FunctionGraphClient.newBuilder() .withCredential(auth) .withRegion(FunctionGraphRegion.valueOf("<region>")) .build(); InvokeFunctionRequest request = new InvokeFunctionRequest(); request.withXCffLogType("tail"); request.withXCFFRequestVersion("v1"); Map<String, Object> listbodyInvokeFunctionRequestBody = new HashMap<>(); listbodyInvokeFunctionRequestBody.put("k", "v"); request.withBody(listbodyInvokeFunctionRequestBody); request.withFunctionUrn("urn:fss:<region>:<project_id>:function:default:<func_name>:<version>"); request.withXCffLogType("tail"); request.withXCFFRequestVersion("v1"); try { InvokeFunctionResponse response = client.invokeFunction(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
AK/SK、region(EndPoint)、project_id(项目ID)的获取请参见AK/SK签名认证操作指导。
func_name、version在函数界面上获取。
xCFFRequestVersionRequest:返回体格式,取值v0,v1。v0:默认返回文本格式;v1:默认返回json格式,sdk需要使用此值。