1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| 'use strict'
const CosSdk = require('cos-nodejs-sdk-v5') const CdnSdk = require('./common/CdnSdk') const CdnRefreshTask = require('./common/CdnRefreshTask') const { getParams, getObjectUrl, logger, getLogSummary } = require('./common/utils')
exports.main_handler = async (event, context, callback) => {
const { objects, cdnHosts, secretId, secretKey, token } = getParams(event)
logger({ title: 'param is parsed success, param as follow: ', data: { objects, cdnHosts, event } })
if (!secretId || !secretKey || !token) { throw new Error(`secretId, secretKey or token is missing`) }
const cdnSdkInstance = new CdnSdk({ secretId, secretKey, token }) const cosInstance = new CosSdk({ SecretId: secretId, SecretKey: secretKey, XCosSecurityToken: token })
const taskList = objects.map(({ bucket, region, key }) => { const purgeUrls = []; cdnHosts.forEach(host => { const tempUrl = getObjectUrl({ cosInstance, bucket, region, key, origin: `${/^(http\:\/\/|https\:\/\/)/.test(host) ? '' : 'https://'}${host}` }); purgeUrls.push(tempUrl); if(tempUrl.lastIndexOf('/index.html') == (tempUrl.length - 11)){ purgeUrls.push(tempUrl.substr(0, tempUrl.length - 10)) } }); return new CdnRefreshTask({ cdnSdkInstance, urls: purgeUrls }) })
const taskResults = [] for (const task of taskList) { const results = await task.runPurgeTasks() taskResults.push(...results) }
logger({ title: 'cdn refresh full logs:', data: taskResults })
const { status, messages } = getLogSummary(taskResults)
logger({ messages: messages.map(item => item.replace(/\,\ /g, '\n')) })
if (status === 'fail') { throw messages.join('; ') } else { return messages.join('; ') } }
|