You can write an SCF function to handle object creation and deletion events in a COS bucket. COS can publish events to the function and invoke it by using the event data as parameters. You can add a bucket notification configuration in the COS bucket, which can identify information such as the trigger event type and name of the function to be invoked.
The COS trigger has the following features:
Push Model
COS monitors specified bucket actions (event types) and invokes related functions, pushing event data to the SCF function. The push model uses bucket notifications to maintain the event source mapping of COS.
Asynchronous Invocation
COS always uses asynchronous invocation to call functions, and the results are not returned to the caller. For more information about invocation types, please refer to Invocation Types.
COS Trigger Attributes
COS bucket (required): The configured COS bucket, which can only be a COS bucket in the same region.
Event type (required): it supports "file upload" and "file deletion" as well as finer-grained upload and deletion events. For specific event types, please see the table below. The event type determines when the trigger triggers the function. For example, if "File upload" is selected, the function will be triggered when there is a file uploaded to the COS bucket.
Event type | Description |
cos:ObjectCreated:* | All upload events mentioned below can trigger the function. |
cos:ObjectCreated:Put | The function will be triggered when a file is created through the Put Object API. |
cos:ObjectCreated:Post | The function will be triggered when a file is created through the Post Object API. |
cos:ObjectCreated:Copy | The function will be triggered when a file is created through the Put Object - Copy API. |
cos:ObjectCreated:CompleteMultipartUpload | The function will be triggered when a file is created through the CompleteMultipartUpload API. |
cos:ObjectCreated:Origin | |
cos:ObjectCreated:Replication | The function will be triggered when an object is created through cross-region replication. |
cos:ObjectRemove:* | All deletion events mentioned below can trigger the function. |
cos:ObjectRemove:Delete | The function will be triggered when an object in a bucket for which versioning is not enabled is deleted through the Delete Object API, or an object on a specified version is deleted with versionid. |
cos:ObjectRemove:DeleteMarkerCreated | The function will be triggered when an object in a bucket for which versioning is enabled or suspended is deleted through the Delete Object API. |
cos:ObjectRestore:Post | The function will be triggered when an archive restoration job is created. |
cos:ObjectRestore:Completed | The function will be triggered when an archive restoration job is completed. |
Prefix filtering (optional): Prefix filtering is usually used to filter file events in a specified directory. For example, if the prefix to be filtered is
test/, only file events in the test/ directory can trigger the function, while those in the hello/ directory cannot.Suffix filtering (optional): Suffix filtering is usually used to filter file events in a specified type or with a specified suffix. For example, if the suffix to be filtered is
.jpg, only file events of the .jpg type can trigger the function, while those of the .png/ type cannot.COS Trigger Use Limits
To avoid errors in the production and delivery of COS events, COS limits each bucket to one triggerable function per event (such as file upload/file deletion) and prefix and suffix filtering combination. Therefore, when creating a COS trigger, do not configure the same rules for the same COS Bucket. For example, you can configure the "Created: *" event trigger for function A for the test Bucket (without configuring filtering rules), then the upload event for this test Bucket cannot be bound to other functions, these events include (Created:Put, Created:Post, etc.), but you can configure the "ObjectRemove" event trigger for function B for the test Bucket.
When using prefix and suffix filtering rules, to ensure the uniqueness of the trigger event for the same Bucket, it is important to note that the same Bucket cannot use overlapping prefixes, overlapping suffixes, or overlapping combinations of prefixes and suffixes to define filtering rules for the same event type. For example, if you have configured the "Created: *" event trigger for function A for the test Bucket with a prefix filter of "Log", then you cannot create another "Created: *" event trigger with a prefix filter of "Log" for this test Bucket.
In addition, COS triggers can only trigger functions in the same region; for example, for an SCF function created in the Guangzhou region, you can only select a COS bucket in the Guangzhou region (South China) when configuring a COS trigger. If you want to trigger a function through COS bucket events in a specific region, create a function in that region.
A COS trigger has limits in two dimensions: SCF and COS, as detailed below:
SCF dimension: one function can be bound to 10 COS triggers at most.
COS dimension: Only one function can be bound to the same event and prefix/suffix rules in a single COS bucket.
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": "NWMxOWY4MGFfMjViMjU4NjRfMTUyMVxxxxxxxxx=","Content-Type": "","x-cos-meta-mykey": "myvalue"},"vid": "","key": "/1253970026/testpic/testfile","size": 1029},"cosBucket": {"region": "cd","name": "testpic","appid": "1253970026"},"cosNotificationId": "unkown"},"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=xxxxxxxxxxxxxx&q-sign-time=1545205709;1545215769&q-key-time=1545205709;1545215769&q-header-list=host;x-cos-storage-class&q-url-param-list=&q-signature=xxxxxxxxxxxxxxx"}},"eventQueue": "qcs:0:scf:cd:appid/1253970026:default.printevent.$LATEST","reservedInfo": "","reqid": 179398952}}]}
The data structures are detailed as below:
Structure | Content |
Records | List structure. There may be multiple messages merged in the list. |
event | This records the event information, including event version, event source, event name, time, queue information, request parameters, and request ID. |
cos | This records the COS information corresponding to the event. |
cosBucket | This records the specific bucket where the event occurred, including the bucket name, region, and the user's APPID. The APPID can be obtained from the Account Information page. |
cosObject | This records the object of the specific event, including object file path, size, custom metadata, and access URL. |
Relevant Examples
The following is a sample COS trigger in Java for your reference:
https://github.com/tencentyun/scf-demo-java/blob/master/src/main/java/example/Cos.java