Using SCF to Automatically Purge COS Resources Cached in CDN
Last updated: 2019-10-25 11:25:04PDF
This document describes how to automatically purge the cached files specified in CDN using Serverless Cloud Function (SCF) when you upload objects to COS, so that CDN can automatically obtain the updated resources.
You need to follow CDN's API request rate limit when using this feature. For more information, see Cache Purge.
When any static content needs to be updated, the old resource in COS will be overwritten by the updated one or deleted. If the configured CDN cache validity period is too long, some edge servers of CDN may still cache the old resource. If the validity period is too short, acceleration may be affected. For more information, see Cache Expiration Configuration.
For this reason, you need to use the cache purge feature in the CDN Console to manually purge the specified URL to delete the invalid cached files or update the resource.
This document describes how to automatically purge cached content in CDN by combining COS with SCF when a file is updated to COS.
- Your Tencent Cloud account needs to have access to COS, CDN, and SCF.
- Follow Creating Buckets and bound the bucket to a CDN accelerated domain name.
- Ensure the region of the COS bucket supports the SCF feature. Cross-region calls are not supported at the moment.
- Prepare the TencentCloud API key for calling the API used to purge cached content in CDN and download the sample code for purging cached content in CDN with SCF.
Creating an SCF Function
The region to which your function belongs must be the same as that of the COS bucket.
- Log in to the SCF Console and click Function Service on the left sidebar.
- Select the same region as for the static content and click Create to create a function.
- On the Create a Function page, select Blank Function, enter the function name (such as refresh_cdn), and then set the runtime environment (the sample code is written in Node.js, so the runtime environment should be set to Nodejs 6.10 here), as shown below:
- After confirming that the configuration is correct, select Next > Finish to create the SCF function.
Configuring the Function
After the blank function is created, you need to add the function code and set the trigger, so that the function can work.
- Configure the function code
- Download the sample code for purging cached content in CDN with SCF.
- Extract all files and then find and open the index.js file.
- In the following code, replace the strings in red boxes with your SecretId and SecretKey (which should have the permission to call the API used for purging cached content in CDN) and the domain name where you want to purge the cached content.
- To purge the cached content at a domain name bound to Tencent Cloud CDN outside Mainland China, change
RefreshCdnUrlin the code to
- Upload the function code
- Compress the modified code and other files into a .zip file.
- In the SCF Console, select the Function Code tab, set the "Submission Method" to "Local Zip Package Upload", click Upload, and select the compressed .zip file as shown below:
- Add a trigger
- In the SCF Console, select the Triggers tab and click Add a Trigger.
- Set Trigger to COS trigger and select the COS bucket where the resource needs to be updated. Below are the configuration items. For more information, see COS Trigger.
COS Bucket: Select the COS bucket to be used as the event source, which must be in the same region as for the function.
Event Type: Select under what conditions the COS bucket will trigger the function. For each COS bucket, only one event type can be set.
If you only want to automatically purge CDN to access the objects uploaded to COS in an overwriting manner, you need to set the "Event Type" to an upload operation, such as creation in the PUT or POST method.
If you also want to automatically purge CDN when a deletion operation occurs, add another trigger and set the "Event Type" to "File Deletion".
Prefix Filtering: prefix filtering is usually used to filter file events in the 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
Suffix Filtering: suffix filtering is usually used to filter file events in the specified type or with the specified suffix. For example, if the suffix to be filtered is
.jpg, only file events of the
.jpgtype can trigger the function, while those of the
- Select "Enable Now".
- After confirming that the configuration information is correct, click Save.
As a CDN operation takes a few minutes to take effect, please wait a moment before you perform a query operation.
After completing the configuration, you can upload a new file with the same object key to the corresponding bucket for verification.
- Log in to the COS Console and upload a new file with the same object key. For directions, see Uploading an Object.
- After uploading, log in to the SCF Console and select Function Service > Function Name > Execution Logs to query the log of successful call.
- Log in to the CDN Console and select Cache Purge > Operation Records to query the record of the automatic call of cache purge.
- If the above test is successful, you can access the CDN-accelerated URL to get the latest resource.