函数操作
更新时间:2024-07-05
各接口的请求参数和响应参数说明请参考函数操作。
创建函数
如下代码可以创建一个CFC函数:
Plain Text
1public void testCreateFunction(CfcClient cfcClient) {
2 String zipFile = "UEsDBBQACAgIAGumvE4AAAAAAAAAAAAAAAAIAAAAaW5kZXgucHkVykEKgCAQQNG94B2m3FQk0i66TeWUgYwxjVG3z/7y8U3l8sVuOcgh3XC+EhJpZcB2FtbkD9onyLLZ8RetPG4QZvIRucEbSfpykeAj7aQVlBglM0E9BIwx1R9QSwcIunbW71oAAABhAAAAUEsBAhQAFAAICAgAa6a8Trp21u9aAAAAYQAAAAgAAAAAAAAAAAAAAAAAAAAAAGluZGV4LnB5UEsFBgAAAAABAAEANgAAAJAAAAAAAA==";
3 Code code = new Code(); //CFC函数代码
4 code.setZipFile(zipFile);
5 code.setPublish(false);
6 code.setDryRun(true);
7 String description = "test api";
8 HashMap<String, String> variables = new HashMap<String, String>();
9 variables.put("additional", "aaa");
10 Environment environment = new Environment();
11 environment.setVariables(variables);
12 long time = System.currentTimeMillis();
13 String functionName = "test-" + String.valueOf(time); //函数名称
14 String handler = "index.handler";
15 Integer memorySize = 256;
16 String runtime = "python2";
17 Integer timeOut = 5;
18
19 CreateFunctionRequest request = new CreateFunctionRequest(functionName, null)
20 .withCode(code)
21 .withEnvironment(environment)
22 .withHandler(handler)
23 .withMemorySize(memorySize)
24 .withRuntime(runtime)
25 .withTimeout(timeOut);
26 CreateFunctionResponse response = cfcClient.createFunction(request);
27 System.out.println(response); //创建成功,获取新建函数信息
28 }
函数列表
如下代码可列出用户所在区域的所有函数:
Plain Text
1public void testListFunctions(CfcClient cfcClient) {
2 ListFunctionsResponse response = cfcClient.listFunctions("ALL", null, null);
3 System.out.println(response);
4 }
获取函数信息
如下代码可以查询用户单个函数:
Plain Text
1public void testGetFunction(CfcClient cfcClient) {
2 String functionName = "test-1559096559957"; //已经存在的函数名称
3 GetFunctionResponse response = cfcClient.getFunction(functionName, "$LATEST");
4 System.out.println(response);
5 }
删除函数
如下代码用于删除用户函数:
Plain Text
1public void testDeleteFunction(CfcClient cfcClient) {
2 String functionName = "test-1557893401910"; //已经存在的函数名称
3 cfcClient.deleteFunction(functionName, null);
4 }
更新函数代码
如下代码用于更新指定函数代码:
Plain Text
1public void testUpdateFunctionCode(CfcClient cfcClient) {
2 String zipFile = "UEsDBBQACAgIAIumvE4AAAAAAAAAAAAAAAAIAAAAaW5kZXgucHkVjMsOgjAQAO9N+g8rXNTYVEHk8TMG6EJJmi2pW5C/t85xMpn8pOMn6GEhjbTBerD1JEUO6qpg9GahuYPIk2r+RgqDE9iejMNwxg2Jb6kixi9fOikgEZBjIMgKwv1t0Tn/KMpn9aqb9t4PYxrMNvsBUEsHCGs/OTtwAAAAdwAAAFBLAQIUABQACAgIAIumvE5rPzk7cAAAAHcAAAAIAAAAAAAAAAAAAAAAAAAAAABpbmRleC5weVBLBQYAAAAAAQABADYAAACmAAAAAAA=";
3 Code code = new Code();
4 code.setZipFile(zipFile);
5 Boolean publish = true;
6 Boolean dryRun = false;
7 String functionName = "test-1559048290101"; //已经存在的函数名称
8
9 UpdateFunctionCodeRequest request = new UpdateFunctionCodeRequest()
10 .withFunctionName(functionName)
11 .withZipFile(zipFile)
12 .withPublish(publish)
13 .withDryRun(dryRun);
14 UpdateFunctionCodeResponse response = cfcClient.updateFunctionCode(request);
15 System.out.println(response);
16
17 }
获取函数配置
如下代码用于获取指定函数的配置信息:
Plain Text
1public void testGetFunctionConfiguration(CfcClient cfcClient) {
2 String functionName = "test-1557387367523"; //已经存在的函数名称
3 GetFunctionConfigurationResponse response = cfcClient.getFunctionConfiguration(functionName, null);
4 System.out.println(response);
5 }
更新函数配置
如下代码用于修改函数配置:
Plain Text
1public void testUpdateFunctionConfiguration(CfcClient cfcClient) {
2 HashMap<String, String> variables = new HashMap<String, String>();
3 variables.put("additional1", "bbbb");
4 Environment environment = new Environment();
5 environment.setVariables(variables);
6 String functionName = "test-1559096559957"; //已经存在的函数名
7 String desc = "123";
8 Integer timeout = 3;
9 String zipFile = "UEsDBBQACAgIAIumvE4AAAAAAAAAAAAAAAAIAAAAaW5kZXgucHkVjMsOgjAQAO9N+g8rXNTYVEHk8TMG6EJJmi2pW5C/t85xMpn8pOMn6GEhjbTBerD1JEUO6qpg9GahuYPIk2r+RgqDE9iejMNwxg2Jb6kixi9fOikgEZBjIMgKwv1t0Tn/KMpn9aqb9t4PYxrMNvsBUEsHCGs/OTtwAAAAdwAAAFBLAQIUABQACAgIAIumvE5rPzk7cAAAAHcAAAAIAAAAAAAAAAAAAAAAAAAAAABpbmRleC5weVBLBQYAAAAAAQABADYAAACmAAAAAAA=";
10 Code code = new Code();
11 code.setZipFile(zipFile);
12 code.setPublish(false);
13 code.setDryRun(true);
14
15 UpdateFunctionConfigurationRequest request = new UpdateFunctionConfigurationRequest(functionName, null)
16 .withEnvironment(environment)
17 .withTimeout(timeout)
18 .withCode(code)
19 .withFunctionName(functionName);
20 UpdateFunctionConfigurationResponse response = cfcClient.updateFunctionConfiguration(request);
21 System.out.println(response);
22 }