背景資訊
事件匯流排EventBridge支援的事件內容轉換類型如下:
完整事件
當轉化類型為完整事件時,事件匯流排EventBridge不對事件進行轉換,將原生CloudEvents的完整結構路由到事件目標。下表介紹經過完整事件轉換前後的事件樣本。
轉換前的事件 | 轉換類型 | 轉換後的事件 |
{
"id":"7adc8c1a-645d-4476-bdef-5d6fb57f****",
"source":"acs.oss",
"specversion":"1.0",
"type":"oss:ObjectCreated:PostObject",
"datacontenttype":"application/json",
"dataschema":"http://example.com/test.json",
"subject":"acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg",
"time":"2020-08-17T16:04:46.149Asia/Shanghai",
"aliyuneventbusname":"demo-bus",
"aliyunregionid":"Shanghai",
"data":{
"name":"test",
"scope":100
}
}
| 完整事件 | {
"id":"7adc8c1a-645d-4476-bdef-5d6fb57f****",
"source":"acs.oss",
"specversion":"1.0",
"type":"oss:ObjectCreated:PostObject",
"datacontenttype":"application/json",
"dataschema":"http://example.com/test.json",
"subject":"acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg",
"time":"2020-08-17T16:04:46.149Asia/Shanghai",
"aliyuneventbusname":"demo-bus",
"aliyunregionid":"Shanghai",
"data":{
"name":"test",
"scope":100
}
}
|
部分事件
當轉換類型為部分事件時,事件匯流排EventBridge通過JSONPath從CloudEvents中提取事件中的參數,將指定的事件內容路由到事件目標。下表介紹經過部分事件轉換前後的事件樣本。
轉換前的事件 | 轉換類型 | 轉換後的事件 |
{
"id":"7adc8c1a-645d-4476-bdef-5d6fb57f****",
"source":"acs.oss",
"specversion":"1.0",
"type":"oss:ObjectCreated:PostObject",
"datacontenttype":"application/json",
"dataschema":"http://example.com/test.json",
"subject":"acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg",
"time":"2020-08-17T16:04:46.149Asia/Shanghai",
"aliyuneventbusname":"demo-bus",
"aliyunregionid":"Shanghai",
"data":{
"name":"test",
"scope":100
}
}
| 部分事件 $.data.name
說明 部分事件只支援定義一個變數。 value不超過10240個字元。
| test
|
常量
當轉換類型為常量時,事件只起到觸發器的作用,不管事件內容是什麼,事件匯流排EventBridge都把常量路由到事件目標。下表介紹經過常量轉換前後的事件樣本。
轉換前的事件 | 轉換類型 | 轉換後的事件 |
{
"id":"7adc8c1a-645d-4476-bdef-5d6fb57f****",
"source":"acs.oss",
"specversion":"1.0",
"type":"oss:ObjectCreated:PostObject",
"datacontenttype":"application/json",
"dataschema":"http://example.com/test.json",
"subject":"acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg",
"time":"2020-08-17T16:04:46.149Asia/Shanghai",
"aliyuneventbusname":"demo-bus",
"aliyunregionid":"Shanghai",
"data":{
"name":"test",
"scope":100
}
}
| 常量 test1
| test1
|
模板
當轉換類型為模板時,通過自訂模板、定義模板裡需要的變數,事件匯流排EventBridge通過JSONPath從CloudEvents提取事件中的參數,把這些值存放在變數中,然後按照模板定義的格式路由到事件目標。下表介紹經過模板轉換前後的事件樣本。
轉換前的事件 | 轉換類型 | 轉換後的事件 |
{
"id":"7adc8c1a-645d-4476-bdef-5d6fb57f****",
"source":"acs.oss",
"specversion":"1.0",
"type":"oss:ObjectCreated:PostObject",
"datacontenttype":"application/json",
"dataschema":"http://example.com/test.json",
"subject":"acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg",
"time":"2020-08-17T16:04:46.149Asia/Shanghai",
"aliyuneventbusname":"demo-bus",
"aliyunregionid":"Shanghai",
"data":{
"name":"test",
"scope":100
}
}
| 模板 變數 {
"name":"$.data.name",
"constant":"Please deal with it timely."
}
模板 The instance is broken,which name is ${name}, ${constant}
| The instance is broken,which name is test, Please deal with it timely.
|
您也可以建立String格式和JSON格式的模板,通過自訂模板將事件轉換成樣本輸出,然後路由到事件目標。以下為事件匯流排EventBridge支援的模板類型:
表 1. 簡單String
轉換前的事件 | 轉換類型 | 轉換後的事件 |
{
"id":"7adc8c1a-645d-4476-bdef-5d6fb57f****",
"source":"acs.oss",
"specversion":"1.0",
"type":"oss:ObjectCreated:PostObject",
"datacontenttype":"application/json",
"dataschema":"http://example.com/test.json",
"subject":"acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg",
"time":"2020-08-17T16:04:46.149Asia/Shanghai",
"aliyuneventbusname":"demo-bus",
"aliyunregionid":"Shanghai",
"data":{
"name":"test",
"state":"RUNNING"
}
}
| 模板 | "name test is in RUNNING"
|
表 2. 簡單JSON
轉換前的事件 | 轉換類型 | 轉換後的事件 |
{
"id":"7adc8c1a-645d-4476-bdef-5d6fb57f****",
"source":"acs.oss",
"specversion":"1.0",
"type":"oss:ObjectCreated:PostObject",
"datacontenttype":"application/json",
"dataschema":"http://example.com/test.json",
"subject":"acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg",
"time":"2020-08-17T16:04:46.149Asia/Shanghai",
"aliyuneventbusname":"demo-bus",
"aliyunregionid":"Shanghai",
"data":{
"name":"test",
"state":"RUNNING"
}
}
| 模板 變數 {
"name":"$.data.name",
"state":"$.data.state"
}
模板 {
"name":"${name}",
"state":"${state}"
}
| {
"name":"test",
"state":"RUNNING"
}
|
表 3. 混合變數和常量的JSON
轉換前的事件 | 轉換類型 | 轉換後的事件 |
{
"id":"7adc8c1a-645d-4476-bdef-5d6fb57f****",
"source":"acs.oss",
"specversion":"1.0",
"type":"oss:ObjectCreated:PostObject",
"datacontenttype":"application/json",
"dataschema":"http://example.com/test.json",
"subject":"acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg",
"time":"2020-08-17T16:04:46.149Asia/Shanghai",
"aliyuneventbusname":"demo-bus",
"aliyunregionid":"Shanghai",
"data":{
"name":"test",
"state":"RUNNING"
}
}
| 模板 變數 {
"name":"$.data.name",
"state":"$.data.state"
}
模板 {
"name":"${name}",
"state":[
9,
"${state}",
true
],
"Transformed":"Yes"
}
| {
"name":"test",
"state":[
9,
"RUNNING",
true
],
"Transformed":"Yes"
}
|
表 4. 嵌套JSON
轉換前的事件 | 轉換類型 | 轉換後的事件 |
{
"id": "45ef4dewdwe1-7c35-447a-bd93-fab****",
"source": "acs.imm",
"specversion": "1.0",
"subject": "acs.imm:cn-hangzhou:123456789098****:215672",
"time": "2020-11-19T21:04:41+08:00",
"type": "imm:FileMeta:Index",
"aliyunaccountid": "123456789098****",
"aliyunpublishtime": "2020-11-19T21:04:42.179PRC",
"aliyuneventbusname": "default",
"aliyunregionid": "cn-hangzhou",
"aliyunpublishaddr": "172.25.XX.XX",
"data": {
"ProjectName": "test_project",
"DatasetName": "test_dataset",
"RequestId": "D2A3AE88-D17C-4CCC-B149-6651115C****",
"StartTime": "2021-07-22T14:03:15.489885+08:00",
"EndTime": "2021-07-22T14:05:15.489885+08:00",
"Message": "InvalidParameter",
"Success": false,
"Files": [
{
"URI": "oss://bucket/file",
"CustomId": "defaultId",
"CustomLabels": {
"Key1": "Val1",
"Key2": "Val2"
},
"Error": "InternalError"
}
]
}
}
| 模板 變數 {
"ProjectName":"$.data.ProjectName",
"Files":"$.data.Files"
}
模板 {
"ProjectName":"${ProjectName}",
"Files":${Files}
}
| {
"ProjectName":"test_project",
"Files": [
{
"URI": "oss://bucket/file",
"CustomId": "defaultId",
"CustomLabels": {
"Key1": "Val1",
"Key2": "Val2"
},
"Error": "InternalError"
}
]
}
|
在模板中支援2個處理特殊字元的函數分別是jsonEscape和htmlEscape。