After you create an Alibaba Cloud Content Delivery Network (CDN) event trigger, you can start writing the code of a function. This topic describes how to write a function in the Function Compute console and use a CDN event trigger to trigger Function Compute to dump CDN offline logs.

Prerequisites

Create a trigger

Procedure

  1. Log on to the Function Compute console.
  2. In the top navigation bar, select your region.
  3. In the left-side navigation pane, click Service/Function.
  4. Find the target function in the target service and click the name of the function.
  5. Click the Code tab and write code in the code editor.

    In this example, the function code is written in Python.

    import json
    import oss2
    import requests
    
    def handler(event, context):
        creds = context.credentials
        auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret, creds.security_token)
        bucket = oss2.Bucket(auth, 'your endpoint', 'your bucket name')
    
      # Parse the event to get the source object info.
      eventObj = json.loads(event)["events"]
      fileUrl = eventObj[0]["eventParameter"]["filePath"]
      file = requests.get(fileUrl)
      result = bucket.put_object("your log name", fileUrl)
    
      return result.status                       

Event formats

The event parameter is an input parameter of Function Compute. The following code shows the specific event formats. The key-value pairs in eventParameter vary with CDN event types.
{
    "events": [
        {
            "eventName": "LogFileCreated",// The type of the event.
            "eventSource": "cdn",// The name of the event source.
            "region": "cn-hangzhou",// The region where the event occurred. Default value: cn-hangzhou.
            "eventVersion": "1.0.0",// The version of the event.
            "eventTime": "2018-06-14T15:31:49+08:00",// The time when the event occurred.
            "traceId": "c6459282-6a4d-4413-894c-e4ea39686738" // The ID passed by the event source. The ID can be used for troubleshooting.
            "userIdentity": {
                "aliUid": "1xxxxxxxxxxxx" // The ID of the user associated with the event.
            },
            "resource": {
                "domain": "example.com"// The name of the domain to which the resource associated with the event belongs.
            },             
            "eventParameter": {
                "key": "value"
            },
        }
    ]
}
  • Example of the LogFileCreated event

    Replace filePath with the path of your CDN log or any test file.

    {
        "events": [
            {
                "eventName": "LogFileCreated",// The type of the event.
                "eventSource": "cdn",// The name of the event source.
                "region": "cn-hangzhou",// The region where the event occurred. Default value: cn-hangzhou.
                "eventVersion": "1.0.0",// The version of the event.
                "eventTime": "2018-06-14T15:31:49+08:00",// The time when the event occurred.
                "traceId": "c6459282-6a4d-4413-894c-e4ea39686738" // The ID passed by the event source. The ID can be used for troubleshooting.
                "userIdentity": {
                    "aliUid": "1xxxxxxxxxxxx" // The ID of the user associated with the event.
                },
                "resource": {
                    "domain": "example.com"// The name of the domain to which the resource associated with the event belongs.
                },
                "eventParameter": {
                    "domain": "example.com",// The name of the domain to which the resource associated with the event belongs.
                    "endTime": 1528959900,// The end time of the log file.
                    "fileSize": 1788115,// The size of the log file.
                    "filePath": "http://cdnlog.cn-hangzhou.oss.aliyun-inc.com/www.aliyun.com/2017_12_27/www.aliyun.com_2017_12_27_0800_0900.gz?OSSAccessKeyId=xxxx&Expires=xxxx&Signature=xxxx",// The path of the log file.
                    "startTime": 1528959600 // The start time of the log file.
                },           
            }
        ]
    }
                        
  • Examples of CachedObjectsRefreshed, CachedObjectsPushed, and CachedObjectsBlocked events
    {
        "events": [
            {
                "eventName": "CachedObjectsRefreshed",// The type of the event.
                "eventVersion": "1.0.0",// The version of the event. The current version is 1.0.0.
                "eventSource": "cdn",// The name of the event source.
                "region": "cn-hangzhou",// The region where the event occurred. Default value: cn-hangzhou.
                "eventTime": "2018-03-16T14:19:55+08:00",// The time when the event occurred.
                "traceId": "cf89e5a8-7d59-4bb5-a33e-4c3d08e25acf",// The ID passed by the event source. The ID can be used for troubleshooting.
                "resource": {
                    "domain": "example.com"// The name of the domain to which the resource associated with the event belongs.
                },
                "eventParameter": {
                    "objectPath": [
                        "/2018/03/16/13/33b430c57e7.mp4",// The ID of the original resource.
                        "/2018/03/16/14/4ff6b9bd54d.mp4"// The ID of the refreshed resource.
                    ],
                    "createTime": 1521180769,// The time when the refresh started.
                    "domain": "example.com",// The name of the domain to which the resource associated with the event belongs.
                    "completeTime": 1521180777,// The time when the refresh ended.
                    "objectType": "File",// The type of the refresh. Valid values: File and Directory.
                    "taskId": 2089687230 //The ID of the resource refresh task.
                },
                "userIdentity": {
                    "aliUid": "1xxxxxxxxxx" // The ID of the user associated with the event.
                }
            }
        ]
    }                    
  • Samples of CdnDomainStarted and CdnDomainStopped events
    {  "events": [
          { 
             "eventName": "CdnDomainStarted",// The type of the event. 
             "eventVersion": "1.0.0",// The version of the event. 
             "eventSource": "cdn",// The name of the event source. 
             "region": "cn-hangzhou",// The region where the event occurred. Default value: cn-hangzhou.
             "eventTime": "2018-03-16T14:19:55+08:00",// The time when the event occurred.
             "traceId": "cf89e5a8-7d59-4bb5-a33e-4c3d08e25acf",// The ID passed by the event source. The ID can be used for troubleshooting.
             "resource": {
                  "domain": "chongshi.alicdn.com" 
             }, 
             "eventParameter": {
                  "domain": "chongshi.alicdn.com", 
                  "status": "online" // The status of the domain. 
             }, 
             "userIdentity": {
                  "aliUid": "12345678"// The ID of the user associated with the event. 
             } 
          } 
       ]
    }
  • Samples of CdnDomainAdded and CdnDomainDeleted events
    {  "events": [
          { 
             "eventName": "CdnDomainStarted",// The type of the event. 
             "eventVersion": "1.0.0",// The version of the event. 
             "eventSource": "cdn",// The name of the event source. 
             "region": "cn-hangzhou",// The region where the event occurred. Default value: cn-hangzhou.
             "eventTime": "2018-03-16T14:19:55+08:00",// The time when the event occurred.
             "traceId": "cf89e5a8-7d59-4bb5-a33e-4c3d08e25acf",// The ID passed by the event source. The ID can be used for troubleshooting.
             "resource": {
                  "domain": "chongshi.alicdn.com" 
             }, 
             "eventParameter": {
                  "domain": "chongshi.alicdn.com", 
             }, 
             "userIdentity": {
                  "aliUid": "12345678"// The ID of the user associated with the event. 
             } 
          } 
       ]
    }

What to do next

Test a function