创建触发器
功能介绍
创建触发器。
- 可以创建的触发器类型包括TIMER、APIG、CTS、DDS、DMS、DIS、LTS、OBS、SMN、KAFKA。(当前OBS触发器暂不支持中国站。)
- DDS和KAFKA触发器创建时默认为DISABLED状态,其他触发器默认为ACTIVE状态。
- TIMER、DDS、DMS、KAFKA、LTS触发器支持禁用,其他触发器不支持。
调用方法
请参见如何调用API。
URI
POST /v2/{project_id}/fgs/triggers/{function_urn}
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
project_id |
是 |
String |
租户项目 ID,获取方式请参见获取项目ID。 |
function_urn |
是 |
String |
函数的URN,详细解释见FunctionGraph函数模型的描述。 |
请求参数
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
X-Auth-Token |
是 |
String |
用户Token。 通过调用IAM服务获取用户Token接口获取(响应消息头中X-Subject-Token的值)。 |
Content-Type |
是 |
String |
消息体的类型(格式) 缺省值:application/json |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
trigger_type_code |
是 |
String |
触发器类型。(当前OBS触发器暂不支持中国站。)
枚举值:
|
trigger_status |
否 |
String |
触发器状态,取值为ACTIVE,DISABLED。 枚举值:
|
event_type_code |
否 |
String |
消息代码。 |
event_data |
是 |
TriggerEventDataRequestBody object |
触发器源事件。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
name |
否 |
String |
|
schedule_type |
否 |
String |
定时触发类型(TIMER触发器参数)。TIMER触发器此参数必填
枚举值:
|
schedule |
否 |
String |
定时触发规则(TIMER触发器参数)。TIMER触发器此参数必填。
|
user_event |
否 |
String |
附加信息(TIMER触发器参数)。 当Timer触发器触发函数执行时,执行事件(函数的event参数)为: {"version": "v1.0", "time": "2018-06-01T08:30:00+08:00", "trigger_type": "TIMER", "trigger_name": "Timer_001", "user_event": "您输入的附加信息"} |
type |
否 |
Integer |
API接口类型(APIG触发器参数)。APIG触发器此参数必填。
|
path |
否 |
String |
APIG接口PATH路径(APIG触发器参数)。APIG触发器此参数必填。 |
protocol |
否 |
String |
API的请求协议(APIG触发器参数)。APIG触发器此参数必填。 枚举值:
|
req_method |
否 |
String |
API的请求方式(APIG触发器参数)。APIG触发器此参数必填。 枚举值:
|
group_id |
否 |
String |
API所属的分组编号(APIG触发器参数)。APIG触发器此参数必填。 |
group_name |
否 |
String |
API所属的分组名称 |
match_mode |
否 |
String |
API的匹配方式(APIG触发器参数)。APIG触发器此参数必填。
枚举值:
|
env_name |
否 |
String |
API的发布环境(APIG触发器参数)。APIG触发器此参数必填。 |
env_id |
否 |
String |
API的发布环境id(APIG触发器参数)。APIG触发器此参数必填。 |
auth |
否 |
String |
API的认证方式(APIG触发器参数)。APIG触发器此参数必填。
枚举值:
|
func_info |
否 |
ApigTriggerFuncInfo object |
APIG触发器函数工作流后端详情(APIG触发器参数)。APIG触发器此参数必填。 |
sl_domain |
否 |
String |
APIG系统默认分配的子域名(APIG触发器参数)。 最小长度:1 最大长度:255 |
backend_type |
否 |
String |
API的后端类型(APIG触发器参数)。 枚举值:
|
operations |
否 |
Array of strings |
自定义操作(CTS触发器参数)。CTS触发器此参数必填。 CTS云审计服务类型和操作订阅所需要的事件通知,当CTS云审计服务获取已订阅的操作记录后,通过CTS触发器将采集到的操作记录作为参数传递来调用FunctionGraph函数。 |
instance_id |
否 |
String |
实例id。DDS、KAFKA、RABBITMQ触发器此参数必填。
|
collection_name |
否 |
String |
集合名称(DDS触发器参数)。DDS触发器此参数必填。 |
db_name |
否 |
String |
文档数据库名称(DDS触发器参数)。DDS触发器此参数必填。 |
db_password |
否 |
String |
文档数据库密码(DDS触发器参数)。DDS触发器此参数必填。 |
batch_size |
否 |
Integer |
批处理大小,单次函数执行处理的最大数据量。DIS、DDS、KAFKA、RABBITMQ触发器此参数必填。
|
queue_id |
否 |
String |
队列id(DMS触发器参数)。DMS触发器此参数必填。 |
consumer_group_id |
否 |
String |
消费组id(DMS触发器参数)。DMS触发器此参数必填。 |
polling_interval |
否 |
Integer |
拉取周期。DIS、DMS触发器此参数必填。 |
stream_name |
否 |
String |
通道名称(DIS触发器参数)。DIS触发器此参数必填。 |
sharditerator_type |
否 |
String |
起始位置(DIS触发器参数)。DIS触发器此参数必填。
枚举值:
|
polling_unit |
否 |
String |
拉取周期单位(DIS触发器参数)。DIS触发器此参数必填。
枚举值:
|
max_fetch_bytes |
否 |
Integer |
最大提取字节数(DIS触发器参数)。 最小值:0 最大值:4194304 |
is_serial |
否 |
String |
串行处理数据(DIS触发器参数),如果开启该选项,取一次数据处理完之后才会取下一次数据;否则只要拉取周期到了就会取数据进行处理。DIS触发器此参数必填。 枚举值:
|
log_group_id |
否 |
String |
日志组id(LTS触发器参数)。LTS触发器此参数必填。 |
log_topic_id |
否 |
String |
日志流id(LTS触发器参数)。LTS触发器此参数必填。 |
bucket |
否 |
String |
桶名称(OBS触发器参数),用作事件源的OBS存储桶,不能和本用户已有桶重名;不能和其他用户已有的桶重名;创建成功后不支持修改。OBS触发器此参数必填。 最小长度:1 最大长度:64 |
prefix |
否 |
String |
前缀(OBS触发器参数),输入一个可选性前缀来限制对以此关键字开头的对象的通知。 最小长度:0 最大长度:1024 |
suffix |
否 |
String |
后缀(OBS触发器参数),输入一个可选性后缀来限制对以此关键字结尾的对象的通知 最小长度:0 最大长度:1024 |
events |
否 |
Array of strings |
触发事件(OBS触发器参数)。OBS触发器此参数必填。
|
topic_urn |
否 |
String |
主题URN(SMN触发器参数)。SMN触发器此参数必填。 最小长度:1 最大长度:255 |
topic_ids |
否 |
Array of strings |
KAFKA主题id列表(KAFKA触发器参数)。KAFKA触发器此参数必填。 |
kafka_user |
否 |
String |
KAFKA账户名(KAFKA触发器参数)。 |
kafka_password |
否 |
String |
KAFKA账户密码(KAFKA触发器参数)。 |
kafka_connect_address |
否 |
String |
KAFKA实例连接IP地址(KAFKA触发器参数)。 |
kafka_ssl_enable |
否 |
Boolean |
KAFKA连接是否开启安全认证(KAFKA触发器参数)。 |
access_password |
否 |
String |
RABBITMQ账户密码(RABBITMQ触发器参数)。RABBITMQ触发器此参数必填。 |
access_user |
否 |
String |
RABBITMQ账户名(RABBITMQ触发器参数)。 |
connect_address |
否 |
String |
实例连接IP地址(RABBITMQ触发器参数)。 |
exchange_name |
否 |
String |
交换机名称(RABBITMQ触发器参数)。RABBITMQ触发器此参数必填。 |
vhost |
否 |
String |
虚拟机名称(RABBITMQ触发器参数)。 |
ssl_enable |
否 |
Boolean |
RABBITMQ连接是否开启安全认证(RABBITMQ触发器参数)。 |
Key_encode |
否 |
Boolean |
EG obs触发器是否对对象加密。 |
agency |
否 |
String |
使用的代理。 |
channel_name |
否 |
String |
通道名称。 |
source_name |
否 |
String |
事件源名称。 |
created_time |
否 |
String |
创建时间。 |
status |
否 |
String |
触发器状态。 枚举值:
|
trigger_name |
否 |
String |
触发器名称。 |
响应参数
状态码: 201
参数 |
参数类型 |
描述 |
---|---|---|
trigger_id |
String |
触发器ID。 |
trigger_type_code |
String |
触发器类型。(当前OBS触发器暂不支持中国站。)
枚举值:
|
trigger_status |
String |
"触发器状态"
枚举值:
|
event_data |
TriggerEventDataResponseBody object |
触发器源事件。 |
last_updated_time |
String |
最后更新时间。 |
created_time |
String |
触发器创建时间。 |
参数 |
参数类型 |
描述 |
---|---|---|
name |
String |
触发器名称 |
schedule_type |
String |
定时触发类型(TIMER触发器参数)。
枚举值:
|
schedule |
String |
定时触发规则(TIMER触发器参数)。
|
user_event |
String |
附加信息(TIMER触发器参数)。 当Timer触发器触发函数执行时,执行事件(函数的event参数)为: {"version": "v1.0", "time": "2018-06-01T08:30:00+08:00", "trigger_type": "TIMER", "trigger_name": "Timer_001", "user_event": "您输入的附加信息"} |
triggerid |
String |
APIG触发器id。(APIG触发器参数) |
type |
Integer |
API接口类型(APIG触发器参数)。
|
path |
String |
APIG接口PATH路径(APIG触发器参数)。 |
protocol |
String |
API的请求协议(APIG触发器参数)。 枚举值:
|
req_method |
String |
API的请求方式(APIG触发器参数)。 枚举值:
|
group_id |
String |
API所属的分组编号(APIG触发器参数)。 |
group_name |
String |
API所属的分组名称(APIG触发器参数)。 |
match_mode |
String |
API的匹配方式(APIG触发器参数)。
枚举值:
|
env_name |
String |
API的发布环境(APIG触发器参数)。 |
env_id |
String |
API的发布环境id(APIG触发器参数)。 |
api_id |
String |
API编号(APIG触发器参数)。 |
api_name |
String |
API名称(APIG触发器参数)。 |
auth |
String |
API的认证方式(APIG触发器参数)。
枚举值:
|
invoke_url |
String |
API调用地址(APIG触发器参数)。 |
func_info |
ApigTriggerFuncInfo object |
APIG触发器函数工作流后端详情(APIG触发器参数)。APIG触发器此参数必填。 |
sl_domain |
String |
APIG系统默认分配的子域名(APIG触发器参数)。 最小长度:1 最大长度:255 |
backend_type |
String |
API的后端类型(APIG触发器参数)。 枚举值:
|
instance_id |
String |
实例id。DDS、KAFKA、RABBITMQ触发器此参数必填。
|
roma_app_id |
String |
API归属的集成应用编号。(APIG触发器参数) |
operations |
Array of strings |
自定义操作(CTS触发器参数)。 CTS云审计服务类型和操作订阅所需要的事件通知,当CTS云审计服务获取已订阅的操作记录后,通过CTS触发器将采集到的操作记录作为参数传递来调用FunctionGraph函数。 |
collection_name |
String |
集合名称(DDS触发器参数)。 |
db_name |
String |
文档数据库名称(DDS触发器参数)。 |
db_password |
String |
文档数据库密码(DDS触发器参数)。 |
db_user |
String |
文档数据库用户名(DDS触发器参数)。 |
instance_addrs |
Array of strings |
文档数据库实例地址(DDS触发器参数)。 |
mode |
String |
文档数据库实例类型(DDS触发器参数)。
|
batch_size |
Integer |
批处理大小,单次函数执行处理的最大数据量。DIS、DDS、KAFKA、RABBITMQ触发器此参数必填。
|
queue_id |
String |
队列id(DMS触发器参数)。 |
consumer_group_id |
String |
消费组id(DMS触发器参数)。 |
polling_interval |
Integer |
拉取周期。 |
stream_name |
String |
通道名称(DIS触发器参数)。 |
sharditerator_type |
String |
起始位置(DIS触发器参数)。
枚举值:
|
polling_unit |
String |
拉取周期单位(DIS触发器参数)。
枚举值:
|
max_fetch_bytes |
Integer |
最大提取字节数(DIS触发器参数)。 最小值:0 最大值:4194304 |
is_serial |
String |
串行处理数据(DIS触发器参数),如果开启该选项,取一次数据处理完之后才会取下一次数据;否则只要拉取周期到了就会取数据进行处理。 枚举值:
|
log_group_id |
String |
日志组id(LTS触发器参数)。 |
log_topic_id |
String |
日志流id(LTS触发器参数)。 |
bucket |
String |
桶名称(OBS触发器参数),用作事件源的OBS存储桶,不能和本用户已有桶重名;不能和其他用户已有的桶重名;创建成功后不支持修改。 最小长度:1 最大长度:64 |
prefix |
String |
前缀(OBS触发器参数),输入一个可选性前缀来限制对以此关键字开头的对象的通知。 最小长度:0 最大长度:1024 |
suffix |
String |
后缀(OBS触发器参数),输入一个可选性后缀来限制对以此关键字结尾的对象的通知 最小长度:0 最大长度:1024 |
events |
Array of strings |
触发事件(OBS触发器参数)。
|
topic_urn |
String |
主题URN(SMN触发器参数)。 最小长度:1 最大长度:255 |
topic_ids |
Array of strings |
KAFKA主题id列表(KAFKA触发器参数)。 |
kafka_user |
String |
KAFKA账户名(KAFKA触发器参数)。 |
kafka_password |
String |
KAFKA账户密码(KAFKA触发器参数)。 |
kafka_connect_address |
String |
KAFKA实例连接IP地址(KAFKA触发器参数)。 |
kafka_ssl_enable |
Boolean |
KAFKA连接是否开启安全认证(KAFKA触发器参数)。 |
access_password |
String |
RABBITMQ账户密码(RABBITMQ触发器参数)。 |
access_user |
String |
RABBITMQ账户名(RABBITMQ触发器参数)。 |
connect_address |
String |
实例连接IP地址(RABBITMQ触发器参数)。 |
exchange_name |
String |
交换机名称(RABBITMQ触发器参数)。 |
vhost |
String |
虚拟机名称(RABBITMQ触发器参数)。 |
ssl_enable |
Boolean |
RABBITMQ连接是否开启安全认证(RABBITMQ触发器参数)。 |
Key_encode |
Boolean |
EG obs触发器是否对对象加密。 |
agency |
String |
使用的代理。 |
channel_name |
String |
通道名称。 |
source_name |
String |
事件源名称。 |
created_time |
String |
创建时间。 |
status |
String |
触发器状态。 枚举值:
|
trigger_name |
String |
触发器名称。 |
参数 |
参数类型 |
描述 |
---|---|---|
function_urn |
String |
函数的URN,详细解释见FunctionGraph函数模型的描述。 |
invocation_type |
String |
调用函数执行方式。
枚举值:
|
timeout |
Integer |
API网关请求函数服务的超时时间(毫秒)。APIG触发器此参数必填。 最小值:1 最大值:60000 |
version |
String |
函数版本信息。 |
状态码: 400
参数 |
参数类型 |
描述 |
---|---|---|
error_code |
String |
错误码 |
error_msg |
String |
错误信息 |
状态码: 401
参数 |
参数类型 |
描述 |
---|---|---|
error_code |
String |
错误码 |
error_msg |
String |
错误信息 |
状态码: 403
参数 |
参数类型 |
描述 |
---|---|---|
error_code |
String |
错误码 |
error_msg |
String |
错误信息 |
状态码: 404
参数 |
参数类型 |
描述 |
---|---|---|
error_code |
String |
错误码 |
error_msg |
String |
错误信息 |
状态码: 500
参数 |
参数类型 |
描述 |
---|---|---|
error_code |
String |
错误码 |
error_msg |
String |
错误信息 |
请求示例
- 创建名为obs-event-1ec7的obs触发器,触发事件为input-picture2桶中有新文件上传。
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "trigger_type_code" : "OBS", "trigger_status" : "ACTIVE", "event_data" : { "bucket" : "input-picture2", "events" : [ "s3:ObjectCreated:*" ], "name" : "obs-event-1ec7" } }
- 创建名为API_test_function的apig共享版触发器,创建无认证方式的临时域名,超时时间为5000s。
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "trigger_type_code" : "APIG", "trigger_status" : "ACTIVE", "event_data" : { "func_info" : { "timeout" : 5000 }, "name" : "API_test_function", "env_id" : "DEFAULT_ENVIRONMENT_RELEASE_ID", "env_name" : "RELEASE", "protocol" : "HTTPS", "auth" : "NONE", "group_id" : "318b09eb214a41488c667ecd51667e3x", "sl_domain" : "318b09eb214a41488c667ecd51667e3x.apig.{region}.example.com", "match_mode" : "SWA", "req_method" : "ANY", "backend_type" : "FUNCTION", "type" : 1, "path" : "/test_gxz_v1_billing" } }
- 创建名为API_test_function的apig专享版触发器,创建无认证方式的临时域名,超时时间为5000s。
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "trigger_type_code" : "DEDICATEDGATEWAY", "trigger_status" : "ACTIVE", "event_data" : { "func_info" : { "timeout" : 5000 }, "name" : "API_test_function", "env_id" : "DEFAULT_ENVIRONMENT_RELEASE_ID", "env_name" : "RELEASE", "protocol" : "HTTPS", "auth" : "IAM", "group_id" : "5106210cf79e4xxxx", "sl_domain" : "5106210cf79e4xxxx.apic.{region_id}.example.com", "match_mode" : "SWA", "req_method" : "ANY", "backend_type" : "FUNCTION", "type" : 1, "path" : "/test_gxz_v1_billing" } }
- 创建86ad3efcxx实例的kafka触发器,从PUSH_TOKEN_RECORD消费组中拉取消息触发函数执行。
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "trigger_type_code" : "KAFKA", "trigger_status" : "DISABLED", "event_data" : { "instance_id" : "86ad3efc-c019-460e-91e7-317b1a89f95x", "topic_ids" : [ "PUSH_TOKEN_RECORD" ], "batch_size" : 100 } }
- 创建timer触发器,每3分钟触发一次函数执行。
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "trigger_type_code" : "TIMER", "trigger_status" : "ACTIVE", "event_data" : { "name" : "Timer-l8v2", "schedule" : "3m", "schedule_type" : "Rate" } }
- 创建指定lts触发器(需配置访问LTS服务权限的委托)。
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "trigger_type_code" : "LTS", "trigger_status" : "ACTIVE", "event_data" : { "log_group_id" : "ccd3fa30-7c86-4590-8a9d-XXX", "log_topic_id" : "5b4944dd-9603-481d-9b91-XXX" } }
- 创建指定smn触发器。
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "trigger_type_code" : "SMN", "trigger_status" : "ACTIVE", "event_data" : { "topic_urn" : "urn:smn:{region}:{project_id}:Failed_times" } }
- 创建名为eg-obs-trigger的EG OBS应用事件源触发器,触发事件为XXX桶中使用Copy方法复制对象(对象名前缀为images/)。
POST https://{Endpoint}/v2/{project_id}/fgs/triggers/{function_urn} { "trigger_type_code": "EVENTGRID", "event_data": { "channel_name": "default", "source_name": "HC.OBS.DWR", "trigger_name": "eg-obs-trigger", "event_types": [ "OBS:DWR:ObjectCreated:COPY" ], "agency": "fgs_to_eg", "bucket": "xxx", "Key_encode": true, "prefix": "images/", "suffix": null }, "trigger_status": "ACTIVE" }
响应示例
状态码: 201
Created
{ "trigger_id" : "9a14fae1-78cf-4185-ac7a-429eb6dc41fb", "trigger_type_code" : "TIMER", "trigger_status" : "ACTIVE", "event_data" : { "name" : "Timer-cpg3", "schedule" : "3m", "schedule_type" : "Rate" }, "last_updated_time" : "2022-11-09 16:37:24", "created_time" : "2022-11-09 16:37:24" }
SDK代码示例
SDK代码示例如下。
- 创建名为obs-event-1ec7的obs触发器,触发事件为input-picture2桶中有新文件上传。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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.*; import java.util.List; import java.util.ArrayList; public class CreateFunctionTriggerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); FunctionGraphClient client = FunctionGraphClient.newBuilder() .withCredential(auth) .withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>")) .build(); CreateFunctionTriggerRequest request = new CreateFunctionTriggerRequest(); CreateFunctionTriggerRequestBody body = new CreateFunctionTriggerRequestBody(); List<String> listEventDataEvents = new ArrayList<>(); listEventDataEvents.add("s3:ObjectCreated:*"); TriggerEventDataRequestBody eventDatabody = new TriggerEventDataRequestBody(); eventDatabody.withName("obs-event-1ec7") .withBucket("input-picture2") .withEvents(listEventDataEvents); body.withEventData(eventDatabody); body.withTriggerStatus(CreateFunctionTriggerRequestBody.TriggerStatusEnum.fromValue("ACTIVE")); body.withTriggerTypeCode(CreateFunctionTriggerRequestBody.TriggerTypeCodeEnum.fromValue("OBS")); request.withBody(body); try { CreateFunctionTriggerResponse response = client.createFunctionTrigger(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.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
- 创建名为API_test_function的apig共享版触发器,创建无认证方式的临时域名,超时时间为5000s。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
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 CreateFunctionTriggerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); FunctionGraphClient client = FunctionGraphClient.newBuilder() .withCredential(auth) .withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>")) .build(); CreateFunctionTriggerRequest request = new CreateFunctionTriggerRequest(); CreateFunctionTriggerRequestBody body = new CreateFunctionTriggerRequestBody(); ApigTriggerFuncInfo funcInfoEventData = new ApigTriggerFuncInfo(); funcInfoEventData.withTimeout(5000); TriggerEventDataRequestBody eventDatabody = new TriggerEventDataRequestBody(); eventDatabody.withName("API_test_function") .withType(1) .withPath("/test_gxz_v1_billing") .withProtocol(TriggerEventDataRequestBody.ProtocolEnum.fromValue("HTTPS")) .withReqMethod(TriggerEventDataRequestBody.ReqMethodEnum.fromValue("ANY")) .withGroupId("318b09eb214a41488c667ecd51667e3x") .withMatchMode(TriggerEventDataRequestBody.MatchModeEnum.fromValue("SWA")) .withEnvName("RELEASE") .withEnvId("DEFAULT_ENVIRONMENT_RELEASE_ID") .withAuth(TriggerEventDataRequestBody.AuthEnum.fromValue("NONE")) .withFuncInfo(funcInfoEventData) .withSlDomain("318b09eb214a41488c667ecd51667e3x.apig.{region}.example.com") .withBackendType(TriggerEventDataRequestBody.BackendTypeEnum.fromValue("FUNCTION")); body.withEventData(eventDatabody); body.withTriggerStatus(CreateFunctionTriggerRequestBody.TriggerStatusEnum.fromValue("ACTIVE")); body.withTriggerTypeCode(CreateFunctionTriggerRequestBody.TriggerTypeCodeEnum.fromValue("APIG")); request.withBody(body); try { CreateFunctionTriggerResponse response = client.createFunctionTrigger(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.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
- 创建名为API_test_function的apig专享版触发器,创建无认证方式的临时域名,超时时间为5000s。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
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 CreateFunctionTriggerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); FunctionGraphClient client = FunctionGraphClient.newBuilder() .withCredential(auth) .withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>")) .build(); CreateFunctionTriggerRequest request = new CreateFunctionTriggerRequest(); CreateFunctionTriggerRequestBody body = new CreateFunctionTriggerRequestBody(); ApigTriggerFuncInfo funcInfoEventData = new ApigTriggerFuncInfo(); funcInfoEventData.withTimeout(5000); TriggerEventDataRequestBody eventDatabody = new TriggerEventDataRequestBody(); eventDatabody.withName("API_test_function") .withType(1) .withPath("/test_gxz_v1_billing") .withProtocol(TriggerEventDataRequestBody.ProtocolEnum.fromValue("HTTPS")) .withReqMethod(TriggerEventDataRequestBody.ReqMethodEnum.fromValue("ANY")) .withGroupId("5106210cf79e4xxxx") .withMatchMode(TriggerEventDataRequestBody.MatchModeEnum.fromValue("SWA")) .withEnvName("RELEASE") .withEnvId("DEFAULT_ENVIRONMENT_RELEASE_ID") .withAuth(TriggerEventDataRequestBody.AuthEnum.fromValue("IAM")) .withFuncInfo(funcInfoEventData) .withSlDomain("5106210cf79e4xxxx.apic.{region_id}.example.com") .withBackendType(TriggerEventDataRequestBody.BackendTypeEnum.fromValue("FUNCTION")); body.withEventData(eventDatabody); body.withTriggerStatus(CreateFunctionTriggerRequestBody.TriggerStatusEnum.fromValue("ACTIVE")); body.withTriggerTypeCode(CreateFunctionTriggerRequestBody.TriggerTypeCodeEnum.fromValue("DEDICATEDGATEWAY")); request.withBody(body); try { CreateFunctionTriggerResponse response = client.createFunctionTrigger(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.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
- 创建86ad3efcxx实例的kafka触发器,从PUSH_TOKEN_RECORD消费组中拉取消息触发函数执行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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.*; import java.util.List; import java.util.ArrayList; public class CreateFunctionTriggerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); FunctionGraphClient client = FunctionGraphClient.newBuilder() .withCredential(auth) .withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>")) .build(); CreateFunctionTriggerRequest request = new CreateFunctionTriggerRequest(); CreateFunctionTriggerRequestBody body = new CreateFunctionTriggerRequestBody(); List<String> listEventDataTopicIds = new ArrayList<>(); listEventDataTopicIds.add("PUSH_TOKEN_RECORD"); TriggerEventDataRequestBody eventDatabody = new TriggerEventDataRequestBody(); eventDatabody.withInstanceId("86ad3efc-c019-460e-91e7-317b1a89f95x") .withBatchSize(100) .withTopicIds(listEventDataTopicIds); body.withEventData(eventDatabody); body.withTriggerStatus(CreateFunctionTriggerRequestBody.TriggerStatusEnum.fromValue("DISABLED")); body.withTriggerTypeCode(CreateFunctionTriggerRequestBody.TriggerTypeCodeEnum.fromValue("KAFKA")); request.withBody(body); try { CreateFunctionTriggerResponse response = client.createFunctionTrigger(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.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
- 创建timer触发器,每3分钟触发一次函数执行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
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 CreateFunctionTriggerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); FunctionGraphClient client = FunctionGraphClient.newBuilder() .withCredential(auth) .withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>")) .build(); CreateFunctionTriggerRequest request = new CreateFunctionTriggerRequest(); CreateFunctionTriggerRequestBody body = new CreateFunctionTriggerRequestBody(); TriggerEventDataRequestBody eventDatabody = new TriggerEventDataRequestBody(); eventDatabody.withName("Timer-l8v2") .withScheduleType(TriggerEventDataRequestBody.ScheduleTypeEnum.fromValue("Rate")) .withSchedule("3m"); body.withEventData(eventDatabody); body.withTriggerStatus(CreateFunctionTriggerRequestBody.TriggerStatusEnum.fromValue("ACTIVE")); body.withTriggerTypeCode(CreateFunctionTriggerRequestBody.TriggerTypeCodeEnum.fromValue("TIMER")); request.withBody(body); try { CreateFunctionTriggerResponse response = client.createFunctionTrigger(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.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
- 创建指定lts触发器(需配置访问LTS服务权限的委托)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
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 CreateFunctionTriggerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); FunctionGraphClient client = FunctionGraphClient.newBuilder() .withCredential(auth) .withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>")) .build(); CreateFunctionTriggerRequest request = new CreateFunctionTriggerRequest(); CreateFunctionTriggerRequestBody body = new CreateFunctionTriggerRequestBody(); TriggerEventDataRequestBody eventDatabody = new TriggerEventDataRequestBody(); eventDatabody.withLogGroupId("ccd3fa30-7c86-4590-8a9d-XXX") .withLogTopicId("5b4944dd-9603-481d-9b91-XXX"); body.withEventData(eventDatabody); body.withTriggerStatus(CreateFunctionTriggerRequestBody.TriggerStatusEnum.fromValue("ACTIVE")); body.withTriggerTypeCode(CreateFunctionTriggerRequestBody.TriggerTypeCodeEnum.fromValue("LTS")); request.withBody(body); try { CreateFunctionTriggerResponse response = client.createFunctionTrigger(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.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
- 创建指定smn触发器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
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 CreateFunctionTriggerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); FunctionGraphClient client = FunctionGraphClient.newBuilder() .withCredential(auth) .withRegion(FunctionGraphRegion.valueOf("<YOUR REGION>")) .build(); CreateFunctionTriggerRequest request = new CreateFunctionTriggerRequest(); CreateFunctionTriggerRequestBody body = new CreateFunctionTriggerRequestBody(); TriggerEventDataRequestBody eventDatabody = new TriggerEventDataRequestBody(); eventDatabody.withTopicUrn("urn:smn:{region}:{project_id}:Failed_times"); body.withEventData(eventDatabody); body.withTriggerStatus(CreateFunctionTriggerRequestBody.TriggerStatusEnum.fromValue("ACTIVE")); body.withTriggerTypeCode(CreateFunctionTriggerRequestBody.TriggerTypeCodeEnum.fromValue("SMN")); request.withBody(body); try { CreateFunctionTriggerResponse response = client.createFunctionTrigger(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.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
- 创建名为obs-event-1ec7的obs触发器,触发事件为input-picture2桶中有新文件上传。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
# 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__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = __import__('os').getenv("CLOUD_SDK_AK") sk = __import__('os').getenv("CLOUD_SDK_SK") credentials = BasicCredentials(ak, sk) \ client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateFunctionTriggerRequest() listEventsEventData = [ "s3:ObjectCreated:*" ] eventDatabody = TriggerEventDataRequestBody( name="obs-event-1ec7", bucket="input-picture2", events=listEventsEventData ) request.body = CreateFunctionTriggerRequestBody( event_data=eventDatabody, trigger_status="ACTIVE", trigger_type_code="OBS" ) response = client.create_function_trigger(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
- 创建名为API_test_function的apig共享版触发器,创建无认证方式的临时域名,超时时间为5000s。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
# 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__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = __import__('os').getenv("CLOUD_SDK_AK") sk = __import__('os').getenv("CLOUD_SDK_SK") credentials = BasicCredentials(ak, sk) \ client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateFunctionTriggerRequest() funcInfoEventData = ApigTriggerFuncInfo( timeout=5000 ) eventDatabody = TriggerEventDataRequestBody( name="API_test_function", type=1, path="/test_gxz_v1_billing", protocol="HTTPS", req_method="ANY", group_id="318b09eb214a41488c667ecd51667e3x", match_mode="SWA", env_name="RELEASE", env_id="DEFAULT_ENVIRONMENT_RELEASE_ID", auth="NONE", func_info=funcInfoEventData, sl_domain="318b09eb214a41488c667ecd51667e3x.apig.{region}.example.com", backend_type="FUNCTION" ) request.body = CreateFunctionTriggerRequestBody( event_data=eventDatabody, trigger_status="ACTIVE", trigger_type_code="APIG" ) response = client.create_function_trigger(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
- 创建名为API_test_function的apig专享版触发器,创建无认证方式的临时域名,超时时间为5000s。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
# 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__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = __import__('os').getenv("CLOUD_SDK_AK") sk = __import__('os').getenv("CLOUD_SDK_SK") credentials = BasicCredentials(ak, sk) \ client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateFunctionTriggerRequest() funcInfoEventData = ApigTriggerFuncInfo( timeout=5000 ) eventDatabody = TriggerEventDataRequestBody( name="API_test_function", type=1, path="/test_gxz_v1_billing", protocol="HTTPS", req_method="ANY", group_id="5106210cf79e4xxxx", match_mode="SWA", env_name="RELEASE", env_id="DEFAULT_ENVIRONMENT_RELEASE_ID", auth="IAM", func_info=funcInfoEventData, sl_domain="5106210cf79e4xxxx.apic.{region_id}.example.com", backend_type="FUNCTION" ) request.body = CreateFunctionTriggerRequestBody( event_data=eventDatabody, trigger_status="ACTIVE", trigger_type_code="DEDICATEDGATEWAY" ) response = client.create_function_trigger(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
- 创建86ad3efcxx实例的kafka触发器,从PUSH_TOKEN_RECORD消费组中拉取消息触发函数执行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
# 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__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = __import__('os').getenv("CLOUD_SDK_AK") sk = __import__('os').getenv("CLOUD_SDK_SK") credentials = BasicCredentials(ak, sk) \ client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateFunctionTriggerRequest() listTopicIdsEventData = [ "PUSH_TOKEN_RECORD" ] eventDatabody = TriggerEventDataRequestBody( instance_id="86ad3efc-c019-460e-91e7-317b1a89f95x", batch_size=100, topic_ids=listTopicIdsEventData ) request.body = CreateFunctionTriggerRequestBody( event_data=eventDatabody, trigger_status="DISABLED", trigger_type_code="KAFKA" ) response = client.create_function_trigger(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
- 创建timer触发器,每3分钟触发一次函数执行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
# 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__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = __import__('os').getenv("CLOUD_SDK_AK") sk = __import__('os').getenv("CLOUD_SDK_SK") credentials = BasicCredentials(ak, sk) \ client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateFunctionTriggerRequest() eventDatabody = TriggerEventDataRequestBody( name="Timer-l8v2", schedule_type="Rate", schedule="3m" ) request.body = CreateFunctionTriggerRequestBody( event_data=eventDatabody, trigger_status="ACTIVE", trigger_type_code="TIMER" ) response = client.create_function_trigger(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
- 创建指定lts触发器(需配置访问LTS服务权限的委托)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
# 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__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = __import__('os').getenv("CLOUD_SDK_AK") sk = __import__('os').getenv("CLOUD_SDK_SK") credentials = BasicCredentials(ak, sk) \ client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateFunctionTriggerRequest() eventDatabody = TriggerEventDataRequestBody( log_group_id="ccd3fa30-7c86-4590-8a9d-XXX", log_topic_id="5b4944dd-9603-481d-9b91-XXX" ) request.body = CreateFunctionTriggerRequestBody( event_data=eventDatabody, trigger_status="ACTIVE", trigger_type_code="LTS" ) response = client.create_function_trigger(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
- 创建指定smn触发器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
# 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__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = __import__('os').getenv("CLOUD_SDK_AK") sk = __import__('os').getenv("CLOUD_SDK_SK") credentials = BasicCredentials(ak, sk) \ client = FunctionGraphClient.new_builder() \ .with_credentials(credentials) \ .with_region(FunctionGraphRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateFunctionTriggerRequest() eventDatabody = TriggerEventDataRequestBody( topic_urn="urn:smn:{region}:{project_id}:Failed_times" ) request.body = CreateFunctionTriggerRequestBody( event_data=eventDatabody, trigger_status="ACTIVE", trigger_type_code="SMN" ) response = client.create_function_trigger(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
- 创建名为obs-event-1ec7的obs触发器,触发事件为input-picture2桶中有新文件上传。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := functiongraph.NewFunctionGraphClient( functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.CreateFunctionTriggerRequest{} var listEventsEventData = []string{ "s3:ObjectCreated:*", } nameEventData:= "obs-event-1ec7" bucketEventData:= "input-picture2" eventDatabody := &model.TriggerEventDataRequestBody{ Name: &nameEventData, Bucket: &bucketEventData, Events: &listEventsEventData, } triggerStatusCreateFunctionTriggerRequestBody:= model.GetCreateFunctionTriggerRequestBodyTriggerStatusEnum().ACTIVE request.Body = &model.CreateFunctionTriggerRequestBody{ EventData: eventDatabody, TriggerStatus: &triggerStatusCreateFunctionTriggerRequestBody, TriggerTypeCode: model.GetCreateFunctionTriggerRequestBodyTriggerTypeCodeEnum().OBS, } response, err := client.CreateFunctionTrigger(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
- 创建名为API_test_function的apig共享版触发器,创建无认证方式的临时域名,超时时间为5000s。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := functiongraph.NewFunctionGraphClient( functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.CreateFunctionTriggerRequest{} funcInfoEventData := &model.ApigTriggerFuncInfo{ Timeout: int32(5000), } nameEventData:= "API_test_function" typeEventData:= int32(1) pathEventData:= "/test_gxz_v1_billing" protocolEventData:= model.GetTriggerEventDataRequestBodyProtocolEnum().HTTPS reqMethodEventData:= model.GetTriggerEventDataRequestBodyReqMethodEnum().ANY groupIdEventData:= "318b09eb214a41488c667ecd51667e3x" matchModeEventData:= model.GetTriggerEventDataRequestBodyMatchModeEnum().SWA envNameEventData:= "RELEASE" envIdEventData:= "DEFAULT_ENVIRONMENT_RELEASE_ID" authEventData:= model.GetTriggerEventDataRequestBodyAuthEnum().NONE slDomainEventData:= "318b09eb214a41488c667ecd51667e3x.apig.{region}.example.com" backendTypeEventData:= model.GetTriggerEventDataRequestBodyBackendTypeEnum().FUNCTION eventDatabody := &model.TriggerEventDataRequestBody{ Name: &nameEventData, Type: &typeEventData, Path: &pathEventData, Protocol: &protocolEventData, ReqMethod: &reqMethodEventData, GroupId: &groupIdEventData, MatchMode: &matchModeEventData, EnvName: &envNameEventData, EnvId: &envIdEventData, Auth: &authEventData, FuncInfo: funcInfoEventData, SlDomain: &slDomainEventData, BackendType: &backendTypeEventData, } triggerStatusCreateFunctionTriggerRequestBody:= model.GetCreateFunctionTriggerRequestBodyTriggerStatusEnum().ACTIVE request.Body = &model.CreateFunctionTriggerRequestBody{ EventData: eventDatabody, TriggerStatus: &triggerStatusCreateFunctionTriggerRequestBody, TriggerTypeCode: model.GetCreateFunctionTriggerRequestBodyTriggerTypeCodeEnum().APIG, } response, err := client.CreateFunctionTrigger(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
- 创建名为API_test_function的apig专享版触发器,创建无认证方式的临时域名,超时时间为5000s。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := functiongraph.NewFunctionGraphClient( functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.CreateFunctionTriggerRequest{} funcInfoEventData := &model.ApigTriggerFuncInfo{ Timeout: int32(5000), } nameEventData:= "API_test_function" typeEventData:= int32(1) pathEventData:= "/test_gxz_v1_billing" protocolEventData:= model.GetTriggerEventDataRequestBodyProtocolEnum().HTTPS reqMethodEventData:= model.GetTriggerEventDataRequestBodyReqMethodEnum().ANY groupIdEventData:= "5106210cf79e4xxxx" matchModeEventData:= model.GetTriggerEventDataRequestBodyMatchModeEnum().SWA envNameEventData:= "RELEASE" envIdEventData:= "DEFAULT_ENVIRONMENT_RELEASE_ID" authEventData:= model.GetTriggerEventDataRequestBodyAuthEnum().IAM slDomainEventData:= "5106210cf79e4xxxx.apic.{region_id}.example.com" backendTypeEventData:= model.GetTriggerEventDataRequestBodyBackendTypeEnum().FUNCTION eventDatabody := &model.TriggerEventDataRequestBody{ Name: &nameEventData, Type: &typeEventData, Path: &pathEventData, Protocol: &protocolEventData, ReqMethod: &reqMethodEventData, GroupId: &groupIdEventData, MatchMode: &matchModeEventData, EnvName: &envNameEventData, EnvId: &envIdEventData, Auth: &authEventData, FuncInfo: funcInfoEventData, SlDomain: &slDomainEventData, BackendType: &backendTypeEventData, } triggerStatusCreateFunctionTriggerRequestBody:= model.GetCreateFunctionTriggerRequestBodyTriggerStatusEnum().ACTIVE request.Body = &model.CreateFunctionTriggerRequestBody{ EventData: eventDatabody, TriggerStatus: &triggerStatusCreateFunctionTriggerRequestBody, TriggerTypeCode: model.GetCreateFunctionTriggerRequestBodyTriggerTypeCodeEnum().DEDICATEDGATEWAY, } response, err := client.CreateFunctionTrigger(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
- 创建86ad3efcxx实例的kafka触发器,从PUSH_TOKEN_RECORD消费组中拉取消息触发函数执行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := functiongraph.NewFunctionGraphClient( functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.CreateFunctionTriggerRequest{} var listTopicIdsEventData = []string{ "PUSH_TOKEN_RECORD", } instanceIdEventData:= "86ad3efc-c019-460e-91e7-317b1a89f95x" batchSizeEventData:= int32(100) eventDatabody := &model.TriggerEventDataRequestBody{ InstanceId: &instanceIdEventData, BatchSize: &batchSizeEventData, TopicIds: &listTopicIdsEventData, } triggerStatusCreateFunctionTriggerRequestBody:= model.GetCreateFunctionTriggerRequestBodyTriggerStatusEnum().DISABLED request.Body = &model.CreateFunctionTriggerRequestBody{ EventData: eventDatabody, TriggerStatus: &triggerStatusCreateFunctionTriggerRequestBody, TriggerTypeCode: model.GetCreateFunctionTriggerRequestBodyTriggerTypeCodeEnum().KAFKA, } response, err := client.CreateFunctionTrigger(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
- 创建timer触发器,每3分钟触发一次函数执行。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := functiongraph.NewFunctionGraphClient( functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.CreateFunctionTriggerRequest{} nameEventData:= "Timer-l8v2" scheduleTypeEventData:= model.GetTriggerEventDataRequestBodyScheduleTypeEnum().RATE scheduleEventData:= "3m" eventDatabody := &model.TriggerEventDataRequestBody{ Name: &nameEventData, ScheduleType: &scheduleTypeEventData, Schedule: &scheduleEventData, } triggerStatusCreateFunctionTriggerRequestBody:= model.GetCreateFunctionTriggerRequestBodyTriggerStatusEnum().ACTIVE request.Body = &model.CreateFunctionTriggerRequestBody{ EventData: eventDatabody, TriggerStatus: &triggerStatusCreateFunctionTriggerRequestBody, TriggerTypeCode: model.GetCreateFunctionTriggerRequestBodyTriggerTypeCodeEnum().TIMER, } response, err := client.CreateFunctionTrigger(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
- 创建指定lts触发器(需配置访问LTS服务权限的委托)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := functiongraph.NewFunctionGraphClient( functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.CreateFunctionTriggerRequest{} logGroupIdEventData:= "ccd3fa30-7c86-4590-8a9d-XXX" logTopicIdEventData:= "5b4944dd-9603-481d-9b91-XXX" eventDatabody := &model.TriggerEventDataRequestBody{ LogGroupId: &logGroupIdEventData, LogTopicId: &logTopicIdEventData, } triggerStatusCreateFunctionTriggerRequestBody:= model.GetCreateFunctionTriggerRequestBodyTriggerStatusEnum().ACTIVE request.Body = &model.CreateFunctionTriggerRequestBody{ EventData: eventDatabody, TriggerStatus: &triggerStatusCreateFunctionTriggerRequestBody, TriggerTypeCode: model.GetCreateFunctionTriggerRequestBodyTriggerTypeCodeEnum().LTS, } response, err := client.CreateFunctionTrigger(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
- 创建指定smn触发器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" functiongraph "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/functiongraph/v2/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := functiongraph.NewFunctionGraphClient( functiongraph.FunctionGraphClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.CreateFunctionTriggerRequest{} topicUrnEventData:= "urn:smn:{region}:{project_id}:Failed_times" eventDatabody := &model.TriggerEventDataRequestBody{ TopicUrn: &topicUrnEventData, } triggerStatusCreateFunctionTriggerRequestBody:= model.GetCreateFunctionTriggerRequestBodyTriggerStatusEnum().ACTIVE request.Body = &model.CreateFunctionTriggerRequestBody{ EventData: eventDatabody, TriggerStatus: &triggerStatusCreateFunctionTriggerRequestBody, TriggerTypeCode: model.GetCreateFunctionTriggerRequestBodyTriggerTypeCodeEnum().SMN, } response, err := client.CreateFunctionTrigger(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。
状态码
状态码 |
描述 |
---|---|
201 |
Created |
400 |
Bad Request 请求错误。 |
401 |
Unauthorized 鉴权失败。 |
403 |
Forbidden 没有操作权限。 |
404 |
Not Found 找不到资源。 |
500 |
Internal Server Error 服务内部错误。 |
错误码
请参见错误码。