COS (Cloud Object Storage)
Event Definition
This example sets up a COS
event which will trigger the hello_world
function whenever an object is uploaded to the cli-appid.cos.ap-beijing.myqcloud.com
under your account.
# serverless.yml
functions:
hello_world:
handler: index.main_handler
runtime: Nodejs8.9
events:
- cos:
name: cli-appid.cos.ap-beijing.myqcloud.com
parameters:
bucket: cli-appid.cos.ap-beijing.myqcloud.com
filter:
prefix: filterdir/
suffix: .jpg
events: cos:ObjectCreated:*
enable: true
-
Use
events
in COS trigger to set the specific trigger event. In the example, the function is called whenever an object is uploaded to the bucket. -
Use filter rules to configure COS trigger, In the example, the function is called whenever an image with
.jpg
extension is uploaded to folderfilterdir
in the bucket.
See the documentation about the COS Trigger to get more detail.
Event Message Structure for COS Trigger
When an object creation or deletion event occurs in the specified COS bucket, event data will be sent to the bound SCF function in JSON format as shown below.
{
"Records": [
{
"cos": {
"cosSchemaVersion": "1.0",
"cosObject": {
"url": "http://testpic-1253970026.cos.ap-chengdu.myqcloud.com/testfile",
"meta": {
"x-cos-request-id": "NWMxOWY4MGFfMjViMjU4NjRfMTUyMV8yNzhhZjM=",
"Content-Type": ""
},
"vid": "",
"key": "/1253970026/testpic/testfile",
"size": 1029
},
"cosBucket": {
"region": "cd",
"name": "testpic",
"appid": "1253970026"
},
"cosNotificationId": "unknown"
},
"event": {
"eventName": "cos:ObjectCreated:*",
"eventVersion": "1.0",
"eventTime": 1545205770,
"eventSource": "qcs::cos",
"requestParameters": {
"requestSourceIP": "192.168.15.101",
"requestHeaders": {
"Authorization": "q-sign-algorithm=sha1&q-ak=AKIDQm6iUh2NJ6jL41tVUis9KpY5Rgv49zyC&q-sign-time=1545205709;1545215769&q-key-time=1545205709;1545215769&q-header-list=host;x-cos-storage-class&q-url-param-list=&q-signature=098ac7dfe9cf21116f946c4b4c29001c2b449b14"
}
},
"eventQueue": "qcs:0:lambda:cd:appid/1253970026:default.printevent.$LATEST",
"reservedInfo": "",
"reqid": 179398952
}
}
]
}