更新时间:2024-10-23 GMT+08:00
分享

创建规则动作

功能介绍

应用服务器可调用此接口在物联网平台创建一条规则动作。

调用方法

请参见如何调用API

URI

POST /v5/iot/{project_id}/routing-rule/actions

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

参数说明:项目ID。获取方法请参见 获取项目ID

请求参数

表2 请求Header参数

参数

是否必选

参数类型

描述

X-Auth-Token

String

参数说明:用户Token。通过调用IAM服务 获取IAM用户Token接口获取,接口返回的响应消息头中“X-Subject-Token”就是需要获取的用户Token。简要的获取方法样例请参见 Token认证

Instance-Id

String

参数说明:实例ID。物理多租下各实例的唯一标识,一般华为云租户无需携带该参数,仅在物理多租场景下从管理面访问API时需要携带该参数。您可以在IoTDA管理控制台界面,选择左侧导航栏“总览”页签查看当前实例的ID。

表3 请求Body参数

参数

是否必选

参数类型

描述

rule_id

String

参数说明:规则触发条件ID,用于唯一标识一条规则触发条件,在创建规则时由物联网平台分配获得。

取值范围:长度不超过36,只允许字母、数字、下划线(_)、连接符(-)的组合。

channel

String

参数说明:规则动作的类型。

取值范围

  • HTTP_FORWARDING:HTTP服务消息类型。

  • DIS_FORWARDING:转发DIS服务消息类型。

  • OBS_FORWARDING:转发OBS服务消息类型。

  • AMQP_FORWARDING:转发AMQP服务消息类型。

  • DMS_KAFKA_FORWARDING:转发kafka消息类型。

  • ROMA_FORWARDING:转发Roma消息类型。(仅企业版支持)

  • INFLUXDB_FORWARDING:转发InfluxDB消息类型。(仅标准版和企业版支持)

  • MYSQL_FORWARDING:转发MySQL消息类型。(仅标准版和企业版支持)

  • FUNCTIONGRAPH_FORWARDING:转发FunctionGraph消息类型。(仅标准版和企业版支持)

  • MRS_KAFKA_FORWARDING:转发MRS_KAFKA消息类型。(仅企业版支持)

  • DMS_ROCKETMQ_FORWARDING:转发RocketMQ消息类型。(仅标准版和企业版支持)

channel_detail

ChannelDetail object

参数说明:通道参数。

表4 ChannelDetail

参数

是否必选

参数类型

描述

http_forwarding

HttpForwarding object

参数说明:http服务器转发消息内容。当channel为HTTP_FORWARDING时,必填。

dis_forwarding

DisForwarding object

参数说明:转发DIS服务消息内容。当channel为DIS_FORWARDING时,必填。

obs_forwarding

ObsForwarding object

参数说明:转发OBS服务消息内容。当channel为OBS_FORWARDING时,必填。

amqp_forwarding

AmqpForwarding object

参数说明:转发AMQP服务消息内容。当channel为AMQP_FORWARDING时,必填。

dms_kafka_forwarding

DmsKafkaForwarding object

参数说明:转发Kafka消息内容。当channel为DMS_KAFKA_FORWARDING时,必填。

roma_forwarding

RomaForwarding object

参数说明:转发Roma消息内容。当channel为ROMA_FORWARDING时,必填。(仅企业版支持)

mysql_forwarding

MysqlForwarding object

参数说明:转发MySQL消息内容。当channel为MYSQL_FORWARDING时,必填。

influxdb_forwarding

InfluxDBForwarding object

参数说明:转发influxdb的配置参数。当channel为INFLUXDB_FORWARDING时,必填。(仅标准版和企业版支持)

functiongraph_forwarding

FunctionGraphForwarding object

参数说明:转发云服务函数服务消息内容。当channel为FUNCTIONGRAPH_FORWARDING时,必填。(仅标准版和企业版支持)

mrs_kafka_forwarding

MrsKafkaForwarding object

参数说明:转发Kafka消息内容。当channel为MRS_KAFKA_FORWARDING时,必填。(仅企业版支持)

dms_rocketmq_forwarding

DmsRocketMQForwarding object

参数说明:转发rocketmq消息内容。当channel为DMS_ROCKETMQ_FORWARDING时,必填。(仅标准版和企业版支持)

表5 HttpForwarding

参数

是否必选

参数类型

描述

url

String

参数说明:用于接收满足规则条件数据的http服务器地址。HTTP为非数据加密传输模式,此模式下数据传输不安全, 建议使用更安全的HTTPS方式

cert_id

String

参数说明:证书id,请参见加载推送证书第3步获取证书ID

cn_name

String

参数说明:当sni_enable为true时,此字段需要填写,内容为将要请求的服务端证书的域名,举例:domain:8443;当sni_enbale为false时,此字段默认不填写。

sni_enable

Boolean

参数说明:需要https服务端和客户端都支持此功能,默认为false,设成true表明Https的客户端在发起请求时,需要携带cn_name;https服务端根据cn_name返回对应的证书;设为false可关闭此功能。

signature_enable

Boolean

参数说明:是否启用签名。填写token时, 该参数必须为true, token才可以生效,否则token不生效。推荐设置成true,使用token签名验证消息是否来自平台。

token

String

参数说明:用作生成签名的Token,客户端可以使用该token按照规则生成签名并与推送消息中携带的签名做对比, 从而验证安全性。取值范围: 长度不超过32, 不小于3, 只允许字母、数字的组合。请参见HTTP/HTTPS推送基于Token认证物联网平台

表6 DisForwarding

参数

是否必选

参数类型

描述

region_name

String

参数说明:DIS服务对应的region区域

project_id

String

参数说明:DIS服务对应的projectId信息

stream_name

String

参数说明:DIS服务对应的通道名称,stream_id和stream_name两个参数必须携带一个,优先使用stream_id

stream_id

String

参数说明:DIS服务对应的通道ID,stream_id和stream_name两个参数必须携带一个,优先使用stream_id

表7 ObsForwarding

参数

是否必选

参数类型

描述

region_name

String

参数说明:OBS服务对应的region区域

project_id

String

参数说明:OBS服务对应的projectId信息

bucket_name

String

参数说明:OBS服务对应的桶名称

location

String

参数说明:OBS服务对应桶的区域

file_path

String

参数说明:OBS服务中存储通道文件的自定义目录,多级目录可用(/)进行分隔,不可以斜杠(/)开头或结尾,不能包含两个以上相邻的斜杠(/)

取值范围: 英文字母(a-zA-Z)、数字(0-9)、下划线(_)、中划线(-)、斜杠(/)和大括号({}),最大字符长度256个字符。其中大括号只能用于对应模板参数。

模板参数:

  • {YYYY} 年

  • {MM} 月

  • {DD} 日

  • {HH} 小时

  • {appId} 应用ID

  • {deviceId} 设备ID

    例如:自定义目录结构为{YYYY}/{MM}/{DD}/{HH},则会在转发数据时,根据当前时间往对应的目录结构2021>08>11>09下生成对应的数据。

表8 AmqpForwarding

参数

是否必选

参数类型

描述

queue_name

String

参数说明:用于接收满足规则条件数据的amqp queue。

表9 DmsKafkaForwarding

参数

是否必选

参数类型

描述

region_name

String

参数说明:Kafka服务对应的region区域

project_id

String

参数说明:Kafka服务对应的projectId信息

addresses

Array of SupportPrivateLinkNetAddress objects

参数说明:转发kafka消息对应的地址列表

topic

String

参数说明:转发kafka消息关联的topic信息。

username

String

参数说明:转发kafka关联的用户名信息。

password

String

参数说明:转发kafka关联的密码信息。

mechanism

String

参数说明:转发kafka关联的SASL认证机制。

取值范围

  • PAAS:明文传输,此模式下为非数据加密传输模式,数据传输不安全,建议您使用更安全的数据加密模式。

  • PLAIN:SASL/PLAIN模式。需要填写对应的用户名密码信息。一种简单的用户名密码校验机制,在SASL_PLAINTEXT场景下,不建议使用。

  • SCRAM-SHA-512:SASL/SCRAM-SHA-512模式。需要填写对应的用户名密码信息。采用哈希算法对用户名与密码生成凭证,进行身份校验的安全认证机制,比PLAIN机制安全性更高。

security_protocol

String

参数说明:kafka传输安全协议,此字段不填默认为SASL_SSL。当mechanism为PAAS或不填时,该字段不生效。

取值范围

  • SASL_SSL:采用SSL证书进行加密传输,支持账号密码认证,安全性更高。

  • SASL_PLAINTEXT:明文传输,支持账号密码认证,性能更好,建议mechanism使用SCRAM-SHA-512机制。

表10 SupportPrivateLinkNetAddress

参数

是否必选

参数类型

描述

ip

String

参数说明:服务的对应IP

port

Integer

参数说明:服务对应端口

domain

String

参数说明:服务对应的域名

表11 RomaForwarding

参数

是否必选

参数类型

描述

addresses

Array of NetAddress objects

参数说明:转发roma消息对应的地址列表

topic

String

参数说明:转发roma消息关联的topic信息。

username

String

参数说明:转发roma关联的用户名信息。

password

String

参数说明:转发roma关联的密码信息。

表12 MysqlForwarding

参数

是否必选

参数类型

描述

address

NetAddress object

转发roma消息对应的地址列表

db_name

String

参数说明:连接MYSQL数据库的库名。

取值范围:长度不超过64,只允许字母、数字、下划线(_)、连接符(-)的组合。

username

String

参数说明:连接MYSQL数据库的用户名

password

String

参数说明:连接MYSQL数据库的密码

enable_ssl

Boolean

参数说明:客户端是否使用SSL连接服务端,默认为true, 若为false,则为非数据加密传输模式,此模式下数据传输不安全,建议您使用SSL模式。

table_name

String

参数说明:MYSQL数据库的表名

column_mappings

Array of ColumnMapping objects

参数说明:MYSQL数据库的列和流转数据的对应关系列表,最多支持32条映射关系。

表13 InfluxDBForwarding

参数

是否必选

参数类型

描述

address

NetAddress object

参数说明:转发InfluxDB消息对应的地址

db_name

String

参数说明:连接InfluxDB数据库的库名,不存在会自动创建

username

String

参数说明:连接InfluxDB数据库的用户名

password

String

参数说明:连接InfluxDB数据库的密码

measurement

String

参数说明:InfluxDB数据库的measurement,不存在会自动创建

column_mappings

Array of ColumnMapping objects

参数说明:InfluxDB数据库和流转数据的对应关系列表,最多支持32条映射关系。

表14 NetAddress

参数

是否必选

参数类型

描述

ip

String

参数说明:服务的对应IP

port

Integer

参数说明:服务对应端口

domain

String

参数说明:服务对应的域名

表15 ColumnMapping

参数

是否必选

参数类型

描述

column_name

String

参数说明:数据库的列名

json_key

String

参数说明:流转数据的属性名

表16 FunctionGraphForwarding

参数

是否必选

参数类型

描述

func_urn

String

参数说明:函数的URN(Uniform Resource Name),唯一标识函数。

func_name

String

参数说明:函数名称。

表17 MrsKafkaForwarding

参数

是否必选

参数类型

描述

addresses

Array of NetAddress objects

参数说明:转发kafka消息对应的地址列表

topic

String

参数说明:转发kafka消息关联的topic信息。

kerberos_authentication

Boolean

是否Kerberos认证,默认为false。

表18 DmsRocketMQForwarding

参数

是否必选

参数类型

描述

addresses

Array of NetAddress objects

参数说明:转发rocketMQ消息对应的地址列表

topic

String

参数说明:转发rocketMQ消息关联的topic信息。

username

String

参数说明:转发rocketMQ关联的用户名信息。

password

String

参数说明:转发rocketMQ关联的密码信息。

enable_ssl

Boolean

是否开启SSL,默认为true。若为false,则为非数据加密传输模式,此模式下数据传输不安全,建议您开启SSL。

响应参数

状态码: 201

表19 响应Body参数

参数

参数类型

描述

action_id

String

规则动作ID,用于唯一标识一条规则动作,在创建规则动作时由物联网平台分配获得,创建时无需携带,由平台统一分配唯一的action_id。

rule_id

String

规则动作对应的的规则触发条件ID。

app_id

String

资源空间ID。

channel

String

规则动作的类型,取值范围:

  • HTTP_FORWARDING:HTTP服务消息类型。

  • DIS_FORWARDING:转发DIS服务消息类型。

  • OBS_FORWARDING:转发OBS服务消息类型。

  • AMQP_FORWARDING:转发AMQP服务消息类型。

  • DMS_KAFKA_FORWARDING:转发kafka消息类型。

  • ROMA_FORWARDING: 转发Roma消息类型。(仅企业版支持)

  • INFLUXDB_FORWARDING:转发InfluxDB消息类型。(仅标准版和企业版支持)

  • MYSQL_FORWARDING:转发MySQL消息类型。(仅标准版和企业版支持)

  • FUNCTIONGRAPH_FORWARDING:转发FunctionGraph消息类型。(仅标准版和企业版支持)

  • MRS_KAFKA_FORWARDING:转发MRS_KAFKA消息类型。(仅企业版支持)

  • DMS_ROCKETMQ_FORWARDING:转发RocketMQ消息类型。(仅标准版和企业版支持)

channel_detail

ChannelDetail object

通道配置信息。

表20 ChannelDetail

参数

参数类型

描述

http_forwarding

HttpForwarding object

参数说明:http服务器转发消息内容。当channel为HTTP_FORWARDING时,必填。

dis_forwarding

DisForwarding object

参数说明:转发DIS服务消息内容。当channel为DIS_FORWARDING时,必填。

obs_forwarding

ObsForwarding object

参数说明:转发OBS服务消息内容。当channel为OBS_FORWARDING时,必填。

amqp_forwarding

AmqpForwarding object

参数说明:转发AMQP服务消息内容。当channel为AMQP_FORWARDING时,必填。

dms_kafka_forwarding

DmsKafkaForwarding object

参数说明:转发Kafka消息内容。当channel为DMS_KAFKA_FORWARDING时,必填。

roma_forwarding

RomaForwarding object

参数说明:转发Roma消息内容。当channel为ROMA_FORWARDING时,必填。(仅企业版支持)

mysql_forwarding

MysqlForwarding object

参数说明:转发MySQL消息内容。当channel为MYSQL_FORWARDING时,必填。

influxdb_forwarding

InfluxDBForwarding object

参数说明:转发influxdb的配置参数。当channel为INFLUXDB_FORWARDING时,必填。(仅标准版和企业版支持)

functiongraph_forwarding

FunctionGraphForwarding object

参数说明:转发云服务函数服务消息内容。当channel为FUNCTIONGRAPH_FORWARDING时,必填。(仅标准版和企业版支持)

mrs_kafka_forwarding

MrsKafkaForwarding object

参数说明:转发Kafka消息内容。当channel为MRS_KAFKA_FORWARDING时,必填。(仅企业版支持)

dms_rocketmq_forwarding

DmsRocketMQForwarding object

参数说明:转发rocketmq消息内容。当channel为DMS_ROCKETMQ_FORWARDING时,必填。(仅标准版和企业版支持)

表21 HttpForwarding

参数

参数类型

描述

url

String

参数说明:用于接收满足规则条件数据的http服务器地址。HTTP为非数据加密传输模式,此模式下数据传输不安全, 建议使用更安全的HTTPS方式

cert_id

String

参数说明:证书id,请参见加载推送证书第3步获取证书ID

cn_name

String

参数说明:当sni_enable为true时,此字段需要填写,内容为将要请求的服务端证书的域名,举例:domain:8443;当sni_enbale为false时,此字段默认不填写。

sni_enable

Boolean

参数说明:需要https服务端和客户端都支持此功能,默认为false,设成true表明Https的客户端在发起请求时,需要携带cn_name;https服务端根据cn_name返回对应的证书;设为false可关闭此功能。

signature_enable

Boolean

参数说明:是否启用签名。填写token时, 该参数必须为true, token才可以生效,否则token不生效。推荐设置成true,使用token签名验证消息是否来自平台。

token

String

参数说明:用作生成签名的Token,客户端可以使用该token按照规则生成签名并与推送消息中携带的签名做对比, 从而验证安全性。取值范围: 长度不超过32, 不小于3, 只允许字母、数字的组合。请参见HTTP/HTTPS推送基于Token认证物联网平台

表22 DisForwarding

参数

参数类型

描述

region_name

String

参数说明:DIS服务对应的region区域

project_id

String

参数说明:DIS服务对应的projectId信息

stream_name

String

参数说明:DIS服务对应的通道名称,stream_id和stream_name两个参数必须携带一个,优先使用stream_id

stream_id

String

参数说明:DIS服务对应的通道ID,stream_id和stream_name两个参数必须携带一个,优先使用stream_id

表23 ObsForwarding

参数

参数类型

描述

region_name

String

参数说明:OBS服务对应的region区域

project_id

String

参数说明:OBS服务对应的projectId信息

bucket_name

String

参数说明:OBS服务对应的桶名称

location

String

参数说明:OBS服务对应桶的区域

file_path

String

参数说明:OBS服务中存储通道文件的自定义目录,多级目录可用(/)进行分隔,不可以斜杠(/)开头或结尾,不能包含两个以上相邻的斜杠(/)

取值范围: 英文字母(a-zA-Z)、数字(0-9)、下划线(_)、中划线(-)、斜杠(/)和大括号({}),最大字符长度256个字符。其中大括号只能用于对应模板参数。

模板参数:

  • {YYYY} 年

  • {MM} 月

  • {DD} 日

  • {HH} 小时

  • {appId} 应用ID

  • {deviceId} 设备ID

    例如:自定义目录结构为{YYYY}/{MM}/{DD}/{HH},则会在转发数据时,根据当前时间往对应的目录结构2021>08>11>09下生成对应的数据。

表24 AmqpForwarding

参数

参数类型

描述

queue_name

String

参数说明:用于接收满足规则条件数据的amqp queue。

表25 DmsKafkaForwarding

参数

参数类型

描述

region_name

String

参数说明:Kafka服务对应的region区域

project_id

String

参数说明:Kafka服务对应的projectId信息

addresses

Array of SupportPrivateLinkNetAddress objects

参数说明:转发kafka消息对应的地址列表

topic

String

参数说明:转发kafka消息关联的topic信息。

username

String

参数说明:转发kafka关联的用户名信息。

password

String

参数说明:转发kafka关联的密码信息。

mechanism

String

参数说明:转发kafka关联的SASL认证机制。

取值范围

  • PAAS:明文传输,此模式下为非数据加密传输模式,数据传输不安全,建议您使用更安全的数据加密模式。

  • PLAIN:SASL/PLAIN模式。需要填写对应的用户名密码信息。一种简单的用户名密码校验机制,在SASL_PLAINTEXT场景下,不建议使用。

  • SCRAM-SHA-512:SASL/SCRAM-SHA-512模式。需要填写对应的用户名密码信息。采用哈希算法对用户名与密码生成凭证,进行身份校验的安全认证机制,比PLAIN机制安全性更高。

security_protocol

String

参数说明:kafka传输安全协议,此字段不填默认为SASL_SSL。当mechanism为PAAS或不填时,该字段不生效。

取值范围

  • SASL_SSL:采用SSL证书进行加密传输,支持账号密码认证,安全性更高。

  • SASL_PLAINTEXT:明文传输,支持账号密码认证,性能更好,建议mechanism使用SCRAM-SHA-512机制。

表26 SupportPrivateLinkNetAddress

参数

参数类型

描述

ip

String

参数说明:服务的对应IP

port

Integer

参数说明:服务对应端口

domain

String

参数说明:服务对应的域名

表27 RomaForwarding

参数

参数类型

描述

addresses

Array of NetAddress objects

参数说明:转发roma消息对应的地址列表

topic

String

参数说明:转发roma消息关联的topic信息。

username

String

参数说明:转发roma关联的用户名信息。

password

String

参数说明:转发roma关联的密码信息。

表28 MysqlForwarding

参数

参数类型

描述

address

NetAddress object

转发roma消息对应的地址列表

db_name

String

参数说明:连接MYSQL数据库的库名。

取值范围:长度不超过64,只允许字母、数字、下划线(_)、连接符(-)的组合。

username

String

参数说明:连接MYSQL数据库的用户名

password

String

参数说明:连接MYSQL数据库的密码

enable_ssl

Boolean

参数说明:客户端是否使用SSL连接服务端,默认为true, 若为false,则为非数据加密传输模式,此模式下数据传输不安全,建议您使用SSL模式。

table_name

String

参数说明:MYSQL数据库的表名

column_mappings

Array of ColumnMapping objects

参数说明:MYSQL数据库的列和流转数据的对应关系列表,最多支持32条映射关系。

表29 InfluxDBForwarding

参数

参数类型

描述

address

NetAddress object

参数说明:转发InfluxDB消息对应的地址

db_name

String

参数说明:连接InfluxDB数据库的库名,不存在会自动创建

username

String

参数说明:连接InfluxDB数据库的用户名

password

String

参数说明:连接InfluxDB数据库的密码

measurement

String

参数说明:InfluxDB数据库的measurement,不存在会自动创建

column_mappings

Array of ColumnMapping objects

参数说明:InfluxDB数据库和流转数据的对应关系列表,最多支持32条映射关系。

表30 NetAddress

参数

参数类型

描述

ip

String

参数说明:服务的对应IP

port

Integer

参数说明:服务对应端口

domain

String

参数说明:服务对应的域名

表31 ColumnMapping

参数

参数类型

描述

column_name

String

参数说明:数据库的列名

json_key

String

参数说明:流转数据的属性名

表32 FunctionGraphForwarding

参数

参数类型

描述

func_urn

String

参数说明:函数的URN(Uniform Resource Name),唯一标识函数。

func_name

String

参数说明:函数名称。

表33 MrsKafkaForwarding

参数

参数类型

描述

addresses

Array of NetAddress objects

参数说明:转发kafka消息对应的地址列表

topic

String

参数说明:转发kafka消息关联的topic信息。

kerberos_authentication

Boolean

是否Kerberos认证,默认为false。

表34 DmsRocketMQForwarding

参数

参数类型

描述

addresses

Array of NetAddress objects

参数说明:转发rocketMQ消息对应的地址列表

topic

String

参数说明:转发rocketMQ消息关联的topic信息。

username

String

参数说明:转发rocketMQ关联的用户名信息。

password

String

参数说明:转发rocketMQ关联的密码信息。

enable_ssl

Boolean

是否开启SSL,默认为true。若为false,则为非数据加密传输模式,此模式下数据传输不安全,建议您开启SSL。

请求示例

  • 创建规则动作,推送至http服务器。

    POST https://{endpoint}/v5/iot/{project_id}/routing-rule/actions
    
    {
      "rule_id" : "1a7ffc5c-d89c-44dd-8265-b1653d951ce0",
      "channel" : "HTTP_FORWARDING",
      "channel_detail" : {
        "http_forwarding" : {
          "url" : "http://host:port/callbackurltest"
        }
      }
    }
  • 创建规则动作,推送至obs。

    POST https://{endpoint}/v5/iot/{project_id}/routing-rule/actions
    
    {
      "rule_id" : "1a7ffc5c-d89c-44dd-8265-b1653d951ce0",
      "channel" : "OBS_FORWARDING",
      "channel_detail" : {
        "obs_forwarding" : {
          "file_path" : "yourPath",
          "project_id" : "yourProjectId",
          "bucket_name" : "yourBucket_name",
          "region_name" : "yourRegion"
        }
      }
    }
  • 创建规则动作,推送至amqp队列。

    POST https://{endpoint}/v5/iot/{project_id}/routing-rule/actions
    
    {
      "rule_id" : "1a7ffc5c-d89c-44dd-8265-b1653d951ce0",
      "channel" : "AMQP_FORWARDING",
      "channel_detail" : {
        "amqp_forwarding" : {
          "queue_name" : "yourQueueName"
        }
      }
    }
  • 创建规则动作,推送至mysql数据库。

    POST https://{endpoint}/v5/iot/{project_id}/routing-rule/actions
    
    {
      "rule_id" : "1a7ffc5c-d89c-44dd-8265-b1653d951ce0",
      "channel" : "MYSQL_FORWARDING",
      "channel_detail" : {
        "mysql_forwarding" : {
          "address" : {
            "ip" : "yourIp",
            "port" : 3306
          },
          "username" : "userName",
          "password" : "passworld",
          "db_name" : "yourDBName",
          "table_name" : "yourTableName",
          "enable_ssl" : true,
          "column_mappings" : [ {
            "column_name" : "serviceId",
            "json_key" : "notify_data.body.services[0].service_id"
          } ]
        }
      }
    }

响应示例

状态码: 201

Created

{
  "action_id" : "1a7ffc5c-d89c-44dd-8265-b1653d951ce1",
  "rule_id" : "1a7ffc5c-d89c-44dd-8265-b1653d951ce0",
  "app_id" : "1a7ffc5cd89c44dd8265b1653d951ce0",
  "channel" : "HTTP_FORWARDING",
  "channel_detail" : {
    "http_forwarding" : {
      "url" : "http://host:port/callbackurltest"
    }
  }
}

SDK代码示例

SDK代码示例如下。

  • 创建规则动作,推送至http服务器。

     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
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.AbstractCredentials;
    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.core.region.Region;
    import com.huaweicloud.sdk.iotda.v5.*;
    import com.huaweicloud.sdk.iotda.v5.model.*;
    
    
    public class CreateRuleActionSolution {
    
        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");
            // ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。
            String iotdaEndpoint = "<YOUR ENDPOINT>";
            String projectId = "{project_id}";
    
            ICredential auth = new BasicCredentials()
                    .withProjectId(projectId)
                    // 标准版/企业版需要使用衍生算法,基础版请删除配置"withDerivedPredicate";
                    .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
                    .withAk(ak)
                    .withSk(sk);
    
            IoTDAClient client = IoTDAClient.newBuilder()
                    .withCredential(auth)
                    // 标准版/企业版:需自行创建Region对象,基础版:请使用IoTDARegion的region对象,如"withRegion(IoTDARegion.CN_NORTH_4)"
                    .withRegion(new Region("cn-north-4", iotdaEndpoint))
                    .build();
            CreateRuleActionRequest request = new CreateRuleActionRequest();
            AddActionReq body = new AddActionReq();
            HttpForwarding httpForwardingChannelDetail = new HttpForwarding();
            httpForwardingChannelDetail.withUrl("http://host:port/callbackurltest");
            ChannelDetail channelDetailbody = new ChannelDetail();
            channelDetailbody.withHttpForwarding(httpForwardingChannelDetail);
            body.withChannelDetail(channelDetailbody);
            body.withChannel("HTTP_FORWARDING");
            body.withRuleId("1a7ffc5c-d89c-44dd-8265-b1653d951ce0");
            request.withBody(body);
            try {
                CreateRuleActionResponse response = client.createRuleAction(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。

     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
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.AbstractCredentials;
    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.core.region.Region;
    import com.huaweicloud.sdk.iotda.v5.*;
    import com.huaweicloud.sdk.iotda.v5.model.*;
    
    
    public class CreateRuleActionSolution {
    
        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");
            // ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。
            String iotdaEndpoint = "<YOUR ENDPOINT>";
            String projectId = "{project_id}";
    
            ICredential auth = new BasicCredentials()
                    .withProjectId(projectId)
                    // 标准版/企业版需要使用衍生算法,基础版请删除配置"withDerivedPredicate";
                    .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
                    .withAk(ak)
                    .withSk(sk);
    
            IoTDAClient client = IoTDAClient.newBuilder()
                    .withCredential(auth)
                    // 标准版/企业版:需自行创建Region对象,基础版:请使用IoTDARegion的region对象,如"withRegion(IoTDARegion.CN_NORTH_4)"
                    .withRegion(new Region("cn-north-4", iotdaEndpoint))
                    .build();
            CreateRuleActionRequest request = new CreateRuleActionRequest();
            AddActionReq body = new AddActionReq();
            ObsForwarding obsForwardingChannelDetail = new ObsForwarding();
            obsForwardingChannelDetail.withRegionName("yourRegion")
                .withProjectId("yourProjectId")
                .withBucketName("yourBucket_name")
                .withFilePath("yourPath");
            ChannelDetail channelDetailbody = new ChannelDetail();
            channelDetailbody.withObsForwarding(obsForwardingChannelDetail);
            body.withChannelDetail(channelDetailbody);
            body.withChannel("OBS_FORWARDING");
            body.withRuleId("1a7ffc5c-d89c-44dd-8265-b1653d951ce0");
            request.withBody(body);
            try {
                CreateRuleActionResponse response = client.createRuleAction(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());
            }
        }
    }
    
  • 创建规则动作,推送至amqp队列。

     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
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.AbstractCredentials;
    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.core.region.Region;
    import com.huaweicloud.sdk.iotda.v5.*;
    import com.huaweicloud.sdk.iotda.v5.model.*;
    
    
    public class CreateRuleActionSolution {
    
        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");
            // ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。
            String iotdaEndpoint = "<YOUR ENDPOINT>";
            String projectId = "{project_id}";
    
            ICredential auth = new BasicCredentials()
                    .withProjectId(projectId)
                    // 标准版/企业版需要使用衍生算法,基础版请删除配置"withDerivedPredicate";
                    .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
                    .withAk(ak)
                    .withSk(sk);
    
            IoTDAClient client = IoTDAClient.newBuilder()
                    .withCredential(auth)
                    // 标准版/企业版:需自行创建Region对象,基础版:请使用IoTDARegion的region对象,如"withRegion(IoTDARegion.CN_NORTH_4)"
                    .withRegion(new Region("cn-north-4", iotdaEndpoint))
                    .build();
            CreateRuleActionRequest request = new CreateRuleActionRequest();
            AddActionReq body = new AddActionReq();
            AmqpForwarding amqpForwardingChannelDetail = new AmqpForwarding();
            amqpForwardingChannelDetail.withQueueName("yourQueueName");
            ChannelDetail channelDetailbody = new ChannelDetail();
            channelDetailbody.withAmqpForwarding(amqpForwardingChannelDetail);
            body.withChannelDetail(channelDetailbody);
            body.withChannel("AMQP_FORWARDING");
            body.withRuleId("1a7ffc5c-d89c-44dd-8265-b1653d951ce0");
            request.withBody(body);
            try {
                CreateRuleActionResponse response = client.createRuleAction(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());
            }
        }
    }
    
  • 创建规则动作,推送至mysql数据库。

     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
    72
    73
    74
    75
    76
    77
    78
    79
    package com.huaweicloud.sdk.test;
    
    import com.huaweicloud.sdk.core.auth.ICredential;
    import com.huaweicloud.sdk.core.auth.AbstractCredentials;
    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.core.region.Region;
    import com.huaweicloud.sdk.iotda.v5.*;
    import com.huaweicloud.sdk.iotda.v5.model.*;
    
    import java.util.List;
    import java.util.ArrayList;
    
    public class CreateRuleActionSolution {
    
        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");
            // ENDPOINT:请在控制台的"总览"界面的"平台接入地址"中查看“应用侧”的https接入地址。
            String iotdaEndpoint = "<YOUR ENDPOINT>";
            String projectId = "{project_id}";
    
            ICredential auth = new BasicCredentials()
                    .withProjectId(projectId)
                    // 标准版/企业版需要使用衍生算法,基础版请删除配置"withDerivedPredicate";
                    .withDerivedPredicate(AbstractCredentials.DEFAULT_DERIVED_PREDICATE) // Used in derivative ak/sk authentication scenarios
                    .withAk(ak)
                    .withSk(sk);
    
            IoTDAClient client = IoTDAClient.newBuilder()
                    .withCredential(auth)
                    // 标准版/企业版:需自行创建Region对象,基础版:请使用IoTDARegion的region对象,如"withRegion(IoTDARegion.CN_NORTH_4)"
                    .withRegion(new Region("cn-north-4", iotdaEndpoint))
                    .build();
            CreateRuleActionRequest request = new CreateRuleActionRequest();
            AddActionReq body = new AddActionReq();
            List<ColumnMapping> listMysqlForwardingColumnMappings = new ArrayList<>();
            listMysqlForwardingColumnMappings.add(
                new ColumnMapping()
                    .withColumnName("serviceId")
                    .withJsonKey("notify_data.body.services[0].service_id")
            );
            NetAddress addressMysqlForwarding = new NetAddress();
            addressMysqlForwarding.withIp("yourIp")
                .withPort(3306);
            MysqlForwarding mysqlForwardingChannelDetail = new MysqlForwarding();
            mysqlForwardingChannelDetail.withAddress(addressMysqlForwarding)
                .withDbName("yourDBName")
                .withUsername("userName")
                .withPassword("passworld")
                .withEnableSsl(true)
                .withTableName("yourTableName")
                .withColumnMappings(listMysqlForwardingColumnMappings);
            ChannelDetail channelDetailbody = new ChannelDetail();
            channelDetailbody.withMysqlForwarding(mysqlForwardingChannelDetail);
            body.withChannelDetail(channelDetailbody);
            body.withChannel("MYSQL_FORWARDING");
            body.withRuleId("1a7ffc5c-d89c-44dd-8265-b1653d951ce0");
            request.withBody(body);
            try {
                CreateRuleActionResponse response = client.createRuleAction(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());
            }
        }
    }
    
  • 创建规则动作,推送至http服务器。

     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
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkcore.auth.credentials import DerivedCredentials
    from huaweicloudsdkcore.region.region import Region as coreRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkiotda.v5 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 = os.environ["CLOUD_SDK_AK"]
        sk = os.environ["CLOUD_SDK_SK"]
        // ENDPOINT请在控制台的"总览"界面的"平台接入地址"中查看应用侧的https接入地址
        iotdaEndpoint = "<YOUR ENDPOINT>";
        projectId = "{project_id}"
    
        credentials = BasicCredentials(ak, sk, projectId).with_derived_predicate(DerivedCredentials.get_default_derived_predicate())
    
        client = IoTDAClient.new_builder() \
            .with_credentials(credentials) \
            # 标准版/企业版:需要使用自行创建的Region对象,基础版:请选择IoTDAClient中的Region对象 如: .with_region(IoTDARegion.CN_NORTH_4)
            .with_region(coreRegion(id="cn-north-4", endpoint=endpoint)) \
            .build()
    
        try:
            request = CreateRuleActionRequest()
            httpForwardingChannelDetail = HttpForwarding(
                url="http://host:port/callbackurltest"
            )
            channelDetailbody = ChannelDetail(
                http_forwarding=httpForwardingChannelDetail
            )
            request.body = AddActionReq(
                channel_detail=channelDetailbody,
                channel="HTTP_FORWARDING",
                rule_id="1a7ffc5c-d89c-44dd-8265-b1653d951ce0"
            )
            response = client.create_rule_action(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。

     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
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkcore.auth.credentials import DerivedCredentials
    from huaweicloudsdkcore.region.region import Region as coreRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkiotda.v5 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 = os.environ["CLOUD_SDK_AK"]
        sk = os.environ["CLOUD_SDK_SK"]
        // ENDPOINT请在控制台的"总览"界面的"平台接入地址"中查看应用侧的https接入地址
        iotdaEndpoint = "<YOUR ENDPOINT>";
        projectId = "{project_id}"
    
        credentials = BasicCredentials(ak, sk, projectId).with_derived_predicate(DerivedCredentials.get_default_derived_predicate())
    
        client = IoTDAClient.new_builder() \
            .with_credentials(credentials) \
            # 标准版/企业版:需要使用自行创建的Region对象,基础版:请选择IoTDAClient中的Region对象 如: .with_region(IoTDARegion.CN_NORTH_4)
            .with_region(coreRegion(id="cn-north-4", endpoint=endpoint)) \
            .build()
    
        try:
            request = CreateRuleActionRequest()
            obsForwardingChannelDetail = ObsForwarding(
                region_name="yourRegion",
                project_id="yourProjectId",
                bucket_name="yourBucket_name",
                file_path="yourPath"
            )
            channelDetailbody = ChannelDetail(
                obs_forwarding=obsForwardingChannelDetail
            )
            request.body = AddActionReq(
                channel_detail=channelDetailbody,
                channel="OBS_FORWARDING",
                rule_id="1a7ffc5c-d89c-44dd-8265-b1653d951ce0"
            )
            response = client.create_rule_action(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 创建规则动作,推送至amqp队列。

     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
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkcore.auth.credentials import DerivedCredentials
    from huaweicloudsdkcore.region.region import Region as coreRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkiotda.v5 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 = os.environ["CLOUD_SDK_AK"]
        sk = os.environ["CLOUD_SDK_SK"]
        // ENDPOINT请在控制台的"总览"界面的"平台接入地址"中查看应用侧的https接入地址
        iotdaEndpoint = "<YOUR ENDPOINT>";
        projectId = "{project_id}"
    
        credentials = BasicCredentials(ak, sk, projectId).with_derived_predicate(DerivedCredentials.get_default_derived_predicate())
    
        client = IoTDAClient.new_builder() \
            .with_credentials(credentials) \
            # 标准版/企业版:需要使用自行创建的Region对象,基础版:请选择IoTDAClient中的Region对象 如: .with_region(IoTDARegion.CN_NORTH_4)
            .with_region(coreRegion(id="cn-north-4", endpoint=endpoint)) \
            .build()
    
        try:
            request = CreateRuleActionRequest()
            amqpForwardingChannelDetail = AmqpForwarding(
                queue_name="yourQueueName"
            )
            channelDetailbody = ChannelDetail(
                amqp_forwarding=amqpForwardingChannelDetail
            )
            request.body = AddActionReq(
                channel_detail=channelDetailbody,
                channel="AMQP_FORWARDING",
                rule_id="1a7ffc5c-d89c-44dd-8265-b1653d951ce0"
            )
            response = client.create_rule_action(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 创建规则动作,推送至mysql数据库。

     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
    # coding: utf-8
    
    import os
    from huaweicloudsdkcore.auth.credentials import BasicCredentials
    from huaweicloudsdkcore.auth.credentials import DerivedCredentials
    from huaweicloudsdkcore.region.region import Region as coreRegion
    from huaweicloudsdkcore.exceptions import exceptions
    from huaweicloudsdkiotda.v5 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 = os.environ["CLOUD_SDK_AK"]
        sk = os.environ["CLOUD_SDK_SK"]
        // ENDPOINT请在控制台的"总览"界面的"平台接入地址"中查看应用侧的https接入地址
        iotdaEndpoint = "<YOUR ENDPOINT>";
        projectId = "{project_id}"
    
        credentials = BasicCredentials(ak, sk, projectId).with_derived_predicate(DerivedCredentials.get_default_derived_predicate())
    
        client = IoTDAClient.new_builder() \
            .with_credentials(credentials) \
            # 标准版/企业版:需要使用自行创建的Region对象,基础版:请选择IoTDAClient中的Region对象 如: .with_region(IoTDARegion.CN_NORTH_4)
            .with_region(coreRegion(id="cn-north-4", endpoint=endpoint)) \
            .build()
    
        try:
            request = CreateRuleActionRequest()
            listColumnMappingsMysqlForwarding = [
                ColumnMapping(
                    column_name="serviceId",
                    json_key="notify_data.body.services[0].service_id"
                )
            ]
            addressMysqlForwarding = NetAddress(
                ip="yourIp",
                port=3306
            )
            mysqlForwardingChannelDetail = MysqlForwarding(
                address=addressMysqlForwarding,
                db_name="yourDBName",
                username="userName",
                password="passworld",
                enable_ssl=True,
                table_name="yourTableName",
                column_mappings=listColumnMappingsMysqlForwarding
            )
            channelDetailbody = ChannelDetail(
                mysql_forwarding=mysqlForwardingChannelDetail
            )
            request.body = AddActionReq(
                channel_detail=channelDetailbody,
                channel="MYSQL_FORWARDING",
                rule_id="1a7ffc5c-d89c-44dd-8265-b1653d951ce0"
            )
            response = client.create_rule_action(request)
            print(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
    
  • 创建规则动作,推送至http服务器。

     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 main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        iotda "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region"
        core_auth "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth"
    )
    
    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")
        // endpoint:请在控制台的"总览"界面的"平台接入地址"中查看"应用侧"的https接入地址
        endpoint := "<YOUR ENDPOINT>"
        projectId := "{project_id}"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            // 企业版/标准版需要使用衍生算法,基础版请删除该配置"WithDerivedPredicate"
            WithDerivedPredicate(core_auth.GetDefaultDerivedPredicate()). // Used in derivative ak/sk authentication scenarios
            Build()
    
        client := iotda.NewIoTDAClient(
            iotda.IoTDAClientBuilder().
                // 标准版/企业版需要自行创建region,基础版使用IoTDARegion中的region对象
                WithRegion(region.NewRegion("cn-north-4", endpoint)).
                WithCredential(auth).
                Build())
    
        request := &model.CreateRuleActionRequest{}
    	httpForwardingChannelDetail := &model.HttpForwarding{
    		Url: "http://host:port/callbackurltest",
    	}
    	channelDetailbody := &model.ChannelDetail{
    		HttpForwarding: httpForwardingChannelDetail,
    	}
    	request.Body = &model.AddActionReq{
    		ChannelDetail: channelDetailbody,
    		Channel: "HTTP_FORWARDING",
    		RuleId: "1a7ffc5c-d89c-44dd-8265-b1653d951ce0",
    	}
    	response, err := client.CreateRuleAction(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 创建规则动作,推送至obs。

     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 main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        iotda "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region"
        core_auth "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth"
    )
    
    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")
        // endpoint:请在控制台的"总览"界面的"平台接入地址"中查看"应用侧"的https接入地址
        endpoint := "<YOUR ENDPOINT>"
        projectId := "{project_id}"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            // 企业版/标准版需要使用衍生算法,基础版请删除该配置"WithDerivedPredicate"
            WithDerivedPredicate(core_auth.GetDefaultDerivedPredicate()). // Used in derivative ak/sk authentication scenarios
            Build()
    
        client := iotda.NewIoTDAClient(
            iotda.IoTDAClientBuilder().
                // 标准版/企业版需要自行创建region,基础版使用IoTDARegion中的region对象
                WithRegion(region.NewRegion("cn-north-4", endpoint)).
                WithCredential(auth).
                Build())
    
        request := &model.CreateRuleActionRequest{}
    	filePathObsForwarding:= "yourPath"
    	obsForwardingChannelDetail := &model.ObsForwarding{
    		RegionName: "yourRegion",
    		ProjectId: "yourProjectId",
    		BucketName: "yourBucket_name",
    		FilePath: &filePathObsForwarding,
    	}
    	channelDetailbody := &model.ChannelDetail{
    		ObsForwarding: obsForwardingChannelDetail,
    	}
    	request.Body = &model.AddActionReq{
    		ChannelDetail: channelDetailbody,
    		Channel: "OBS_FORWARDING",
    		RuleId: "1a7ffc5c-d89c-44dd-8265-b1653d951ce0",
    	}
    	response, err := client.CreateRuleAction(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 创建规则动作,推送至amqp队列。

     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 main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        iotda "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region"
        core_auth "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth"
    )
    
    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")
        // endpoint:请在控制台的"总览"界面的"平台接入地址"中查看"应用侧"的https接入地址
        endpoint := "<YOUR ENDPOINT>"
        projectId := "{project_id}"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            // 企业版/标准版需要使用衍生算法,基础版请删除该配置"WithDerivedPredicate"
            WithDerivedPredicate(core_auth.GetDefaultDerivedPredicate()). // Used in derivative ak/sk authentication scenarios
            Build()
    
        client := iotda.NewIoTDAClient(
            iotda.IoTDAClientBuilder().
                // 标准版/企业版需要自行创建region,基础版使用IoTDARegion中的region对象
                WithRegion(region.NewRegion("cn-north-4", endpoint)).
                WithCredential(auth).
                Build())
    
        request := &model.CreateRuleActionRequest{}
    	amqpForwardingChannelDetail := &model.AmqpForwarding{
    		QueueName: "yourQueueName",
    	}
    	channelDetailbody := &model.ChannelDetail{
    		AmqpForwarding: amqpForwardingChannelDetail,
    	}
    	request.Body = &model.AddActionReq{
    		ChannelDetail: channelDetailbody,
    		Channel: "AMQP_FORWARDING",
    		RuleId: "1a7ffc5c-d89c-44dd-8265-b1653d951ce0",
    	}
    	response, err := client.CreateRuleAction(request)
    	if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
    
  • 创建规则动作,推送至mysql数据库。

     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
    72
    73
    package main
    
    import (
    	"fmt"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
        iotda "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5"
    	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/iotda/v5/model"
        region "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/region"
        core_auth "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth"
    )
    
    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")
        // endpoint:请在控制台的"总览"界面的"平台接入地址"中查看"应用侧"的https接入地址
        endpoint := "<YOUR ENDPOINT>"
        projectId := "{project_id}"
    
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            WithProjectId(projectId).
            // 企业版/标准版需要使用衍生算法,基础版请删除该配置"WithDerivedPredicate"
            WithDerivedPredicate(core_auth.GetDefaultDerivedPredicate()). // Used in derivative ak/sk authentication scenarios
            Build()
    
        client := iotda.NewIoTDAClient(
            iotda.IoTDAClientBuilder().
                // 标准版/企业版需要自行创建region,基础版使用IoTDARegion中的region对象
                WithRegion(region.NewRegion("cn-north-4", endpoint)).
                WithCredential(auth).
                Build())
    
        request := &model.CreateRuleActionRequest{}
    	var listColumnMappingsMysqlForwarding = []model.ColumnMapping{
            {
                ColumnName: "serviceId",
                JsonKey: "notify_data.body.services[0].service_id",
            },
        }
    	ipAddress:= "yourIp"
    	portAddress:= int32(3306)
    	addressMysqlForwarding := &model.NetAddress{
    		Ip: &ipAddress,
    		Port: &portAddress,
    	}
    	enableSslMysqlForwarding:= true
    	mysqlForwardingChannelDetail := &model.MysqlForwarding{
    		Address: addressMysqlForwarding,
    		DbName: "yourDBName",
    		Username: "userName",
    		Password: "passworld",
    		EnableSsl: &enableSslMysqlForwarding,
    		TableName: "yourTableName",
    		ColumnMappings: listColumnMappingsMysqlForwarding,
    	}
    	channelDetailbody := &model.ChannelDetail{
    		MysqlForwarding: mysqlForwardingChannelDetail,
    	}
    	request.Body = &model.AddActionReq{
    		ChannelDetail: channelDetailbody,
    		Channel: "MYSQL_FORWARDING",
    		RuleId: "1a7ffc5c-d89c-44dd-8265-b1653d951ce0",
    	}
    	response, err := client.CreateRuleAction(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

错误码

请参见错误码

相关文档