更新时间:2024-08-09 GMT+08:00

事件内容转换

事件网格通过事件内容转换将CloudEvents标准事件转换成事件目标可以处理的类型。

事件网格支持的转换类型如下:透传、变量、常量。

透传

事件网格不对事件进行转换,将CloudEvents标准事件直接路由到事件目标。示例如下:

转换前的事件

转换类型

转换后的事件

{
    "events":[{
	"id": "4b26115b-73e-cf74a******",
        "specversion": "1.0",
	"source": "HC.OBS",
	"type": "object:put",
	"datacontenttype": "application/json",
	"subject": "xxx.jpg",
	"time": "2022-01-17T12:07:48.955Z",
	"data": {
		"name": "test01",
		"state": "enable"
	}
    }]
}

透传

{
    "events":[{
	"id": "4b26115b-73e-cf74******",
        "specversion": "1.0",
	"source": "HC.OBS",
	"type": "object:put",
	"datacontenttype": "application/json",
	"subject": "xxx.jpg",
	"time": "2022-01-17T12:07:48.955Z",
	"data": {
		"name": "test01",
		"state": "enable"
	}
    }]
}

变量

从CloudEvents标准事件中获取变量值,将变量值按照模板定义的格式路由到事件目标。示例如下:

转换前的事件

转换类型

转换后的事件

{
    "events":[{
	"id": "4b26115b-73e-cf74a******",
        "specversion": "1.0",
	"source": "HC.OBS",
	"type": "object:put",
	"datacontenttype": "application/json",
	"subject": "xxx.jpg",
	"time": "2022-01-17T12:07:48.955Z",
	"data": {
		"name": "test01",
		"state": "enable"
	}
    }]
}

参数

{"name":"$.data.name"}

模板

My name is ${name}
说明:

当事件目标为“FunctionGraph(函数计算)”,模板需为JSON格式,示例如下:

{"name":"${name}"}
My name is test01
说明:

当事件目标为“FunctionGraph(函数计算)”,转换后的事件结果如下:

{"name": "test01"}

例子: 从OBS->EG->FG全链路中复杂转换内容举例:

转换前的事件

转换类型

转换后的事件

{
    "specversion": "1.0",
    "id": "******9db447aa3******",
    "source": "HC.OBS.DWR",
    "type": "OBS:DWR:ObjectCreated:PUT",
    "datacontenttype": "application/json",
    "dataschema": "",
    "subject": "test.txt",
    "time": "2023-08-01T11:41:51.712759419Z",
    "ttl": "4000",
    "data": {
        "eventVersion": "3.0",
        "eventSource": "OBS",
        "eventRegion": "cn-north-4",
        "eventTime": "2023-08-01T19:41:47.879Z",
        "eventName": "ObjectCreated:Put",
        "userIdentity": {
            "ID": "******fef0f08c******"
        },
        "requestParameters": {
            "sourceIPAddress": "1**.1**.1**.1**"
        },
        "responseElements": {
            "x-obs-request-id": "******47aa3cdafb******",
            "x-obs-id-2": "",
            "x-amz-request-id": "",
            "x-amz-id-2": ""
        },
        "obs": {
            "Version": "1.0",
            "configurationId": "******4aaac1******",
            "bucket": {
                "name": "test",
                "ownerIdentity": {
                    "ID": "******f1234567******"
                },
                "bucket": "test",
                "arn": ""
            },
            "object": {
                "key": "test.txt",
                "eTag": "******48ce552c3******",
                "size": 13,
                "versionId": "******BE7FFFF******",
                "sequencer": "1",
                "oldpsxpth": ""
            }
        }
    }
}

参数

{
  "eventVersion": "$.data.eventVersion",
  "eventTime": "$.data.eventTime",
  "requestParameters": "$.data.requestParameters.sourceIPAddress",
  "configurationId": "$.data.obs.configurationId",
  "eTag": "$.data.obs.object.eTag",
  "sequencer": "$.data.obs.object.sequencer",
  "key": "$.data.obs.object.key",
  "size": "$.data.obs.object.size",
  "arn": "$.data.obs.bucket.arn",
  "name": "$.data.obs.bucket.name",
  "ownerIdentity": "$.data.obs.bucket.ownerIdentity.ID",
  "Region": "$.data.eventRegion",
  "eventName": "$.type",
  "userIdentity": "$.data.userIdentity.ID"
}

模板

{
    "Records": [
        {
            "eventVersion": "${eventVersion}",
            "eventTime": "${eventTime}",
            "requestParameters": {
                "sourceIPAddress": "${requestParameters}"
            },
            "obs": {
                "configurationId": "${configurationId}",
                "object": {
                    "eTag": "${eTag}",
                    "sequencer": "${sequencer}",
                    "key": "${key}",
                    "size": "${size}"
                },
                "bucket": {
                    "arn": "${arn}",
                    "name": "${name}",
                    "ownerIdentity": {
                        "PrincipalId": "${ownerIdentity}"
                    }
                }
            },
            "Region": "${Region}",
            "eventName": "${eventName}",
            "userIdentity": {
                "principalId": "${userIdentity}"
            }
        }
    ]
}
说明:

模板的value值是取参数的key值.

{
    "Records": [
        {
            "eventVersion": "3.0",
            "eventTime": "2023-08-01T19:41:47.879Z",
            "requestParameters": {
                "sourceIPAddress": "1**.1**.1**.1**"
            },
            "obs": {
                "configurationId": "******4aaac1******",
                "object": {
                    "eTag": "******48ce552c3******",
                    "sequencer": "1",
                    "key": "test.txt",
                    "size": "13"
                },
                "bucket": {
                    "arn": "",
                    "name": "test",
                    "ownerIdentity": {
                        "PrincipalId": "******f1234567******"
                    }
                }
            },
            "Region": "cn-north-4",
            "eventName": "OBS:DWR:ObjectCreated:PUT",
            "userIdentity": {
                "principalId": "******fef0f08c******"
            }
        }
    ]
}

常量

事件只能触发事件目标,但是不会传送事件内容到事件目标,事件网格将您设置的常量路由到事件目标。示例如下:

转换前的事件

规则配置

转换后的事件

{
    "events":[{
	"id": "4b26115b-73cf74a******",
        "specversion": "1.0",
	"source": "HC.OBS",
	"type": "object:put",
	"datacontenttype": "application/json",
	"subject": "xxx.jpg",
	"time": "2022-01-17T12:07:48.955Z",
	"data": {
		"name": "test01",
		"state": "enable"
	}
    }]
}

参数

test01
说明:

当事件目标为“FunctionGraph(函数计算)”,规则配置参数需为JSON格式,示例如下:

{"name": "test01"}
test01
说明:

当事件目标为“FunctionGraph(函数计算)”,转换后的事件结果如下:

{"name": "test01"}

其他示例

  1. 在创建事件订阅-配置事件源为“分布式消息服务RabbitMQ版”或“分布式消息服务RocketMQ版”,消息转换为CloudEvents格式事件后,data字段下将包含context字段,在创建事件订阅-配置事件目标,规则类型配置为“变量”时,规则必须包含context字段,示例如下:

    转换前的事件

    转换类型

    转换后的事件

    {
        "type": "ROCKETMQ:CloudTrace:RocketmqCall",
        "data": {
            "context": {    
    		"name": "test01",
    		"state": "enable"
             }
        },
        "source": "zhang_roc",
        "time": "2023-02-01T10:47:07Z",
        "datacontenttype": "application/json",
        "specversion": "1.0",
        "id": "2f885496-570c-4925-82fd-d1ad09******",
        "subject": "ROCKETMQ:cn-north-7:eec88b34-9470-483e-8961-edb168******/0de095e33e00d36e2fd2c0019a******:ROCKETMQ:zhang_roc"
    }

    参数

    {"name":"$.data.context.name"}

    模板

    My name is ${name}
    My name is test01
  2. 在创建事件订阅-配置事件目标,事件目标配置为“FunctionGraph(函数计算)”时,若内容转换规则配置为“透传”,无法将事件内容作为输入值传递至事件目标。如需将事件内容作为函数流输入值,可以通过“变量”或“常量”的内容转换规则,规则必须包含input字段,示例如下:
    表1 变量示例

    转换前的事件

    规则配置

    转换后的事件

    {
        "events":[{
    	"id": "4b26115b-73cf74a******",
            "specversion": "1.0",
    	"source": "HC.OBS",
    	"type": "object:put",
    	"datacontenttype": "application/json",
    	"subject": "xxx.jpg",
    	"time": "2022-01-17T12:07:48.955Z",
    	"data": {
    		"name": "test01",
    		"state": "enable"
    	}
        }]
    }

    变量

    {"data": "$.data"}

    模板

    {"input": ${data}}

    {
    	"input": {
    		"name": "test01",
    		"state": "enable"
    	}
    }
    表2 常量示例

    转换前的事件

    规则配置

    转换后的事件

    {
        "events":[{
    	"id": "4b26115b-73cf74a******",
            "specversion": "1.0",
    	"source": "HC.OBS",
    	"type": "object:put",
    	"datacontenttype": "application/json",
    	"subject": "xxx.jpg",
    	"time": "2022-01-17T12:07:48.955Z",
    	"data": {
    		"name": "test01",
    		"state": "enable"
    	}
        }]
    }

    常量

    {
    	"input": {
    		"name": "test01"
    	}
    }
    {
    	"input": {
    		"name": "test01"
    	}
    }