更新时间:2023-11-07 GMT+08:00

Python SDK使用范例

以同步执行函数为例,提供封装后的SDK。为了保证您成功使用以下示例代码,您需要先添加对应语言的SDK依赖。

表1 Python SDK信息说明

SDK信息

说明

版本

3.0.97

安装

pip install huaweicloudsdkfunctiongraph

相关链接

SDK依赖包地址

Python SDK使用说明

SDK的请求参数、响应参数、请求示例和响应示例与对应的API一致,具体参数和示例说明请参见同步执行函数接口。

SDK调用示例
# 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>"
 
    credentials = BasicCredentials(ak, sk) \
 
    client = FunctionGraphClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(FunctionGraphRegion.value_of("<region>")) \
        .build()
 
    try:
        request = InvokeFunctionRequest()
        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在函数界面上获取。

X-CFF-Request-Version:返回体格式,取值v0,v1。v0:默认返回文本格式;v1:默认返回json格式,sdk需要使用此值。