函数操作
更新时间:2024-07-05
各接口的请求参数和响应参数说明请参考函数操作。
创建函数
如下代码可以创建一个CFC函数:
Plain Text
1 function_name = "testHelloWorld" #您可以自定义您的函数名
2 # 创建一个cfc客户端
3 #有三个endpoint可以选择:cfc.bj.baidubce.com,cfc.gz.baidubce.com,cfc.su.baidubce.com
4 host = '<Your Endpoint>'
5 AK = '<Your AccessKeyID>'
6 SK = '<Your AccessKeySecret>'
7
8 # 也可以通过环境变量的形式获取ak,sk
9 # AK = os.environ["BCE_ACCESS_KEY_ID"]
10 # SK = os.environ["BCE_ACCESS_KEY_SECRET"]
11
12 config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=host)
13 cfc_client = CfcClient(config)
14
15#下面是您要发布的zip包的 base64-encoded, 注意zip包压缩时不能包含顶层文件夹目录
16 base64_file = '<Your base64-encoded Code>'
17
18 #创建函数
19 create_response = cfc_client.create_function(function_name,
20 description="CFC SDK Demo",
21 handler="<Your index>.handler",
22 memory_size=128,
23 region='bj',
24 zip_file=base64_file,
25 publish=False,
26 run_time='python2',
27 timeout=3,
28 dry_run=False)
函数列表
如下代码可列出用户所在区域的所有函数:
Plain Text
1# list functions
2list_functions_response = cfc_client.list_functions()
获取函数信息
如下代码可以查询用户单个函数:
Plain Text
1get_function_response = cfc_client.get_function(FunctionName)
更新函数代码
如下代码用于更新指定函数代码:
Plain Text
1brn = create_response.FunctionBrn
2update_function_code_response = cfc_client.update_function_code(brn,
3 zip_file=base64_file_with_args,
4 publish=True)
5
获取函数配置
如下代码用于获取指定函数的配置信息:
Plain Text
1// 根据函数BRN获取配置信息,也可以根据函数名称获取配置信息
2brn = create_response.FunctionBrn
3get_function_configuration_response = cfc_client.get_function_configuration(brn)
更新函数配置
如下代码用于修改函数配置:
Plain Text
1brn = create_response.FunctionBrn
2// 更新函数描述信息和环境变量
3response = cfc_client.update_function_configuration(brn, description="update config",
4 environment={"e1": "1"})
删除函数
如下代码用于删除用户函数:
Plain Text
1 delete_function_response = cfc_client.delete_function(function_name)