Python SDK使用范例
以同步执行函数为例,提供封装后的SDK。为了保证您成功使用以下示例代码,您需要先添加对应语言的SDK依赖。
SDK信息 |
说明 |
---|---|
安装 |
pip install huaweicloudsdkfunctiongraph |
相关链接 |
SDK的请求参数、响应参数、请求示例和响应示例与对应的API一致,具体参数和示例说明请参见同步执行函数接口。
# coding: utf-8 from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkfunctiongraph.v2.region.functiongraph_region import FunctionGraphRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkfunctiongraph.v2 import * if __name__ == "__main__": # 该示例仅为测试使用,在生产环境中请勿硬编码ak sk 信息在代码中 ak = "<YOUR AK>" sk = "<YOUR SK>" security_token = os.getenv("HUAWEICLOUD_SDK_SECURITY_TOKEN") project_id = "{your projectId string}" credentials = BasicCredentials(ak, sk, project_id).with_security_token(security_token) \ client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<region>")) \ .build() try: request = InvokeFunctionRequest() request.x_cff_log_type = "tail" request.x_cff_request_version = "v1" request.function_urn = "urn:fss:<region>:<project_id>:function:default:<func_name>:<version>" response = client.invoke_function(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
AK/SK、region(EndPoint)、project_id(项目ID)的获取请参见AK/SK签名认证操作指导。
func_name、version在函数界面上获取。
xCFFRequestVersionRequest:返回体格式,取值v0,v1。v0:默认返回文本格式;v1:默认返回json格式,sdk需要使用此值。