更新时间:2024-11-29 GMT+08:00
分享

创建触发器

功能介绍

创建触发器。

  • 可以创建的触发器类型包括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}

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

租户项目 ID,获取方式请参见获取项目ID

function_urn

String

函数的URN,详细解释见FunctionGraph函数模型的描述。

请求参数

表2 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

用户Token。

通过调用IAM服务获取用户Token接口获取(响应消息头中X-Subject-Token的值)。

Content-Type

String

消息体的类型(格式)

缺省值:application/json

表3 请求Body参数

参数

是否必选

参数类型

描述

trigger_type_code

String

触发器类型。(当前OBS触发器暂不支持中国站。)

  • TIMER: 定时触发器。
  • APIG: APIGW触发器。
  • CTS: 云审计触发器,需要先开通云审计服务。
  • DDS: 文档数据库触发器,需要开启函数vpc。
  • DMS: 分布式消息服务触发器,需要配置dms委托。
  • DIS: 数据接入服务触发器,需要配置dis委托。
  • LTS: 云审计日志服务触发器,需要配置lts委托。
  • OBS: 对象存储服务触发器。
  • KAFKA: 专享版本kafka触发器。

枚举值:

  • TIMER
  • APIG
  • CTS
  • DDS
  • DMS
  • DIS
  • LTS
  • OBS
  • SMN
  • KAFKA
  • RABBITMQ
  • DEDICATEDGATEWAY
  • OPENSOURCEKAFKA
  • APIC
  • GeminiDB Mongo
  • EVENTGRID
  • IOTDA

trigger_status

String

触发器状态,取值为ACTIVE,DISABLED。

枚举值:

  • ACTIVE
  • DISABLED

event_type_code

String

消息代码。

event_data

TriggerEventDataRequestBody object

触发器源事件。

表4 TriggerEventDataRequestBody

参数

是否必选

参数类型

描述

name

String

  • TIMER触发器:触发器名称
  • APIG触发器:API名称
  • CTS触发器:通知名称
  • OBS触发器:事件通知名称,默认值为触发器id

schedule_type

String

定时触发类型(TIMER触发器参数)。TIMER触发器此参数必填

  • Rate:指定固定频率(分钟、小时、天数)定期调用函数,单位为分钟时,输入值不能超过60;单位为小时时,输入值不能超过24;单位为天时,输入值不能超过30。
  • Cron:指定Cron表达式定期调用函数

枚举值:

  • Rate
  • Cron

schedule

String

定时触发规则(TIMER触发器参数)。TIMER触发器此参数必填。

  • 触发类型为Rate时对应定时规则
  • 触发类型为Cron时对应Cron表达式

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触发器此参数必填。

  • 1:公有API
  • 2:私有API

path

String

APIG接口PATH路径(APIG触发器参数)。APIG触发器此参数必填。

protocol

String

API的请求协议(APIG触发器参数)。APIG触发器此参数必填。

枚举值:

  • HTTP
  • HTTPS

req_method

String

API的请求方式(APIG触发器参数)。APIG触发器此参数必填。

枚举值:

  • GET
  • POST
  • PUT
  • DELETE
  • HEAD
  • PATCH
  • OPTIONS
  • ANY

group_id

String

API所属的分组编号(APIG触发器参数)。APIG触发器此参数必填。

group_name

String

API所属的分组名称

match_mode

String

API的匹配方式(APIG触发器参数)。APIG触发器此参数必填。

  • SWA:前缀匹配
  • NORMAL:正常匹配(绝对匹配)

枚举值:

  • SWA
  • NORMAL

env_name

String

API的发布环境(APIG触发器参数)。APIG触发器此参数必填。

env_id

String

API的发布环境id(APIG触发器参数)。APIG触发器此参数必填。

auth

String

API的认证方式(APIG触发器参数)。APIG触发器此参数必填。

  • IAM:IAM认证,只允许IAM用户能访问,安全级别中等
  • APP:采用Appkey&Appsecret认证,安全级别高,推荐使用
  • NONE:无认证模式,所有用户均可访问,不推荐使用

枚举值:

  • IAM
  • APP
  • NONE

func_info

ApigTriggerFuncInfo object

APIG触发器函数工作流后端详情(APIG触发器参数)。APIG触发器此参数必填。

sl_domain

String

APIG系统默认分配的子域名(APIG触发器参数)。

最小长度:1

最大长度:255

backend_type

String

API的后端类型(APIG触发器参数)。

枚举值:

  • FUNCTION

operations

Array of strings

自定义操作(CTS触发器参数)。CTS触发器此参数必填。 CTS云审计服务类型和操作订阅所需要的事件通知,当CTS云审计服务获取已订阅的操作记录后,通过CTS触发器将采集到的操作记录作为参数传递来调用FunctionGraph函数。

instance_id

String

实例id。DDS、KAFKA、RABBITMQ触发器此参数必填。

  • APIG触发器:apig实例id
  • DDS触发器:文档数据库实例id
  • KAFKA触发器:KAFKA实例id
  • RABBITMQ触发器:RABBITMQ实例id

collection_name

String

集合名称(DDS触发器参数)。DDS触发器此参数必填。

db_name

String

文档数据库名称(DDS触发器参数)。DDS触发器此参数必填。

db_password

String

文档数据库密码(DDS触发器参数)。DDS触发器此参数必填。

batch_size

Integer

批处理大小,单次函数执行处理的最大数据量。DIS、DDS、KAFKA、RABBITMQ触发器此参数必填。

  • DDS触发器:批处理大小设置1-10,000的范围内
  • DIS触发器:批处理大小设置1-10,000的范围内
  • KAFKA触发器:批处理大小设置1-1,000的范围内
  • RABBITMQ触发器:批处理大小设置1-1,000的范围内

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触发器此参数必填。

  • TRIM_HORIZON:从最早被存储至分区的有效记录开始读取。
  • LATEST:从分区中的最新记录开始读取,此设置可以保证总是读到分区中最新记录。

枚举值:

  • TRIM_HORIZON
  • LATEST

polling_unit

String

拉取周期单位(DIS触发器参数)。DIS触发器此参数必填。

  • s:秒
  • ms:毫秒

枚举值:

  • s
  • ms

max_fetch_bytes

Integer

最大提取字节数(DIS触发器参数)。

最小值:0

最大值:4194304

is_serial

String

串行处理数据(DIS触发器参数),如果开启该选项,取一次数据处理完之后才会取下一次数据;否则只要拉取周期到了就会取数据进行处理。DIS触发器此参数必填。

枚举值:

  • true
  • false

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触发器此参数必填。

  • ObjectCreated:表示所有创建对象的操作,包含Put、Post、Copy对象以及合并段
  • Put:使用Put方法上传对象
  • Post:使用Post方法上传对象
  • Copy:使用copy方法复制对象
  • CompleteMultipartUpload:表示合并分段任务
  • ObjectRemoved:表示删除对象
  • Delete:指定对象版本号删除对象
  • DeleteMarkerCreated:不指定对象版本号删除对象

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触发器是否对对象加密(EVENTGRID触发器参数)。

agency

String

使用的代理(EVENTGRID触发器参数)。

channel_name

String

通道名称(EVENTGRID触发器参数)。

channel_id

String

通道id(EVENTGRID触发器参数)。

source_name

String

事件源名称(EVENTGRID触发器参数)。

created_time

String

创建时间(EVENTGRID触发器参数)。

status

String

触发器状态(EVENTGRID触发器参数)。

枚举值:

  • ACTIVE
  • DISABLE

trigger_name

String

触发器名称(EVENTGRID触发器参数)。

event_types

Array of strings

事件类型(EVENTGRID触发器参数)。例如:OBS:DWR:ObjectCreated:COPY。

表5 ApigTriggerFuncInfo

参数

是否必选

参数类型

描述

function_urn

String

函数的URN,详细解释见FunctionGraph函数模型的描述。

invocation_type

String

调用函数执行方式。

  • sync:同步执行
  • async:异步执行

枚举值:

  • sync
  • async

timeout

Integer

API网关请求函数服务的超时时间(毫秒)。APIG触发器此参数必填。

最小值:1

最大值:60000

version

String

函数版本信息。

响应参数

状态码: 201

表6 响应Body参数

参数

参数类型

描述

trigger_id

String

触发器ID。

trigger_type_code

String

触发器类型。(当前OBS触发器暂不支持中国站。)

  • TIMER: "定时触发器。"
  • APIG: "APIG触发器。"
  • CTS: "云审计服务触发器。"
  • DDS: "文档数据库服务触发器。"
  • DMS: "分布式服务触发器。"
  • DIS: "数据接入服务触发器。"
  • LTS: "云日志服务触发器。"
  • OBS: "对象存储触发器。"
  • SMN: "消息通知服务触发器。"
  • KAFKA: "专享版消息通知服务触发器。"

枚举值:

  • TIMER
  • APIG
  • CTS
  • DDS
  • DMS
  • DIS
  • LTS
  • OBS
  • SMN
  • KAFKA
  • RABBITMQ
  • DEDICATEDGATEWAY
  • OPENSOURCEKAFKA
  • APIC
  • GeminiDB Mongo
  • EVENTGRID
  • IOTDA

trigger_status

String

"触发器状态"

  • ACTIVE: 启用状态。
  • DISABLED: 禁用状态。

枚举值:

  • ACTIVE
  • DISABLED

event_data

TriggerEventDataResponseBody object

触发器源事件。

last_updated_time

String

最后更新时间。

created_time

String

触发器创建时间。

表7 TriggerEventDataResponseBody

参数

参数类型

描述

name

String

触发器名称

schedule_type

String

定时触发类型(TIMER触发器参数)。

  • Rate:指定固定频率(分钟、小时、天数)定期调用函数,单位为分钟时,输入值不能超过60;单位为小时时,输入值不能超过24;单位为天时,输入值不能超过30。
  • Cron:指定Cron表达式定期调用函数

枚举值:

  • Rate
  • Cron

schedule

String

定时触发规则(TIMER触发器参数)。

  • 触发类型为Rate时对应定时规则
  • 触发类型为Cron时对应Cron表达式

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触发器参数)。

  • 1:公有API
  • 2:私有API

path

String

APIG接口PATH路径(APIG触发器参数)。

protocol

String

API的请求协议(APIG触发器参数)。

枚举值:

  • HTTP
  • HTTPS

req_method

String

API的请求方式(APIG触发器参数)。

枚举值:

  • GET
  • POST
  • PUT
  • DELETE
  • HEAD
  • PATCH
  • OPTIONS
  • ANY

group_id

String

API所属的分组编号(APIG触发器参数)。

group_name

String

API所属的分组名称(APIG触发器参数)。

match_mode

String

API的匹配方式(APIG触发器参数)。

  • SWA:前缀匹配
  • NORMAL:正常匹配(绝对匹配)

枚举值:

  • SWA
  • NORMAL

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触发器参数)。

  • IAM:IAM认证,只允许IAM用户能访问,安全级别中等
  • APP:采用Appkey&Appsecret认证,安全级别高,推荐使用
  • NONE:无认证模式,所有用户均可访问,不推荐使用

枚举值:

  • IAM
  • APP
  • NONE

invoke_url

String

API调用地址(APIG触发器参数)。

func_info

ApigTriggerFuncInfo object

APIG触发器函数工作流后端详情(APIG触发器参数)。APIG触发器此参数必填。

sl_domain

String

APIG系统默认分配的子域名(APIG触发器参数)。

最小长度:1

最大长度:255

backend_type

String

API的后端类型(APIG触发器参数)。

枚举值:

  • FUNCTION

instance_id

String

实例id。DDS、KAFKA、RABBITMQ触发器此参数必填。

  • APIG触发器:apig实例id
  • DDS触发器:文档数据库实例id
  • KAFKA触发器:KAFKA实例id
  • RABBITMQ触发器:RABBITMQ实例id

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触发器参数)。

  • Sharding:集群实例
  • ReplicaSet:副本集实例
  • Single:单节点实例

batch_size

Integer

批处理大小,单次函数执行处理的最大数据量。DIS、DDS、KAFKA、RABBITMQ触发器此参数必填。

  • DDS触发器:批处理大小设置1-10,000的范围内
  • DIS触发器:批处理大小设置1-10,000的范围内
  • KAFKA触发器:批处理大小设置1-1,000的范围内
  • RABBITMQ触发器:批处理大小设置1-1,000的范围内

queue_id

String

队列id(DMS触发器参数)。

consumer_group_id

String

消费组id(DMS触发器参数)。

polling_interval

Integer

拉取周期。

stream_name

String

通道名称(DIS触发器参数)。

sharditerator_type

String

起始位置(DIS触发器参数)。

  • TRIM_HORIZON:从最早被存储至分区的有效记录开始读取。
  • LATEST:从分区中的最新记录开始读取,此设置可以保证总是读到分区中最新记录。

枚举值:

  • TRIM_HORIZON
  • LATEST

polling_unit

String

拉取周期单位(DIS触发器参数)。

  • s:秒
  • ms:毫秒

枚举值:

  • s
  • ms

max_fetch_bytes

Integer

最大提取字节数(DIS触发器参数)。

最小值:0

最大值:4194304

is_serial

String

串行处理数据(DIS触发器参数),如果开启该选项,取一次数据处理完之后才会取下一次数据;否则只要拉取周期到了就会取数据进行处理。

枚举值:

  • true
  • false

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触发器参数)。

  • ObjectCreated:表示所有创建对象的操作,包含Put、Post、Copy对象以及合并段
  • Put:使用Put方法上传对象
  • Post:使用Post方法上传对象
  • Copy:使用copy方法复制对象
  • CompleteMultipartUpload:表示合并分段任务
  • ObjectRemoved:表示删除对象
  • Delete:指定对象版本号删除对象
  • DeleteMarkerCreated:不指定对象版本号删除对象

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触发器是否对对象加密(EVENTGRID触发器参数)。

agency

String

使用的代理(EVENTGRID触发器参数)。

channel_name

String

通道名称(EVENTGRID触发器参数)。

channel_id

String

通道id(EVENTGRID触发器参数)。

source_name

String

事件源名称(EVENTGRID触发器参数)。

created_time

String

创建时间(EVENTGRID触发器参数)。

status

String

触发器状态(EVENTGRID触发器参数)。

枚举值:

  • ACTIVE
  • DISABLE

trigger_name

String

触发器名称(EVENTGRID触发器参数)。

event_types

Array of strings

事件类型(EVENTGRID触发器参数)。例如:OBS:DWR:ObjectCreated:COPY。

表8 ApigTriggerFuncInfo

参数

参数类型

描述

function_urn

String

函数的URN,详细解释见FunctionGraph函数模型的描述。

invocation_type

String

调用函数执行方式。

  • sync:同步执行
  • async:异步执行

枚举值:

  • sync
  • async

timeout

Integer

API网关请求函数服务的超时时间(毫秒)。APIG触发器此参数必填。

最小值:1

最大值:60000

version

String

函数版本信息。

状态码: 400

表9 响应Body参数

参数

参数类型

描述

error_code

String

错误码

error_msg

String

错误信息

状态码: 401

表10 响应Body参数

参数

参数类型

描述

error_code

String

错误码

error_msg

String

错误信息

状态码: 403

表11 响应Body参数

参数

参数类型

描述

error_code

String

错误码

error_msg

String

错误信息

状态码: 404

表12 响应Body参数

参数

参数类型

描述

error_code

String

错误码

error_msg

String

错误信息

状态码: 500

表13 响应Body参数

参数

参数类型

描述

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",
        "channel_id": "xxx",
        "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 服务内部错误。

错误码

请参见错误码

相关文档