Help Center/ EventGrid/ User Guide/ Event Rules/ Event Content Transformation
Updated on 2024-09-06 GMT+08:00

Event Content Transformation

EG transforms CloudEvents-compliant events so that they can be processed by specified targets.

Supported transform types: pass-through, variables, constants.

Pass-through

Directly route CloudEvents-compliant events to the target. Example:

Before Transformation

Transform Type

After Transformation

{
    "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"
	}
    }]
}

Pass-through

{
    "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"
	}
    }]
}

Variables

Route variables in CloudEvents-compliant events to the target by using a template. Example:

Before Transformation

Transform Type

After Transformation

{
    "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"
	}
    }]
}

Parameter

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

Template

My name is ${name}
NOTE:

If the event target is FunctionGraph (function computing), the template must be in JSON format. Example:

{"name":"${name}"}
My name is test01
NOTE:

If the event target is FunctionGraph (function computing), the transformation result is as follows:

{"name": "test01"}

Example of complex transformation from OBS to EG and then to FunctionGraph:

Before Transformation

Transform Type

After Transformation

{
    "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": ""
            }
        }
    }
}

Parameters

{
  "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"
}

Template

{
    "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}"
            }
        }
    ]
}
NOTE:

The value in the template is the key of the corresponding parameter.

{
    "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******"
            }
        }
    ]
}
NOTE:

Constants

Route constants in events to the target. Example:

Before Transformation

Rule

After Transformation

{
    "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"
	}
    }]
}

Parameter

test01
NOTE:

If the event target is FunctionGraph (function computing), the rule must be in JSON format. Example:

{"name": "test01"}
test01
NOTE:

If the event target is FunctionGraph (function computing), the transformation result is as follows:

{"name": "test01"}

More Examples

  1. After you set a DMS for RabbitMQ or DMS for RocketMQ event source for a subscription, messages will contain the context field in data after being transformed to CloudEvents-compliant events. If you set the transform type to Variables for the event target, the rule must also contain the context field. Example:

    Before Transformation

    Transform Type

    After Transformation

    {
        "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"
    }

    Parameter

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

    Template

    My name is ${name}
    My name is test01