如果你发现AWS CloudFront没有更新,可以尝试以下解决方法:
方法一:清除缓存
// 创建AWS CloudFront对象
const cloudfront = new AWS.CloudFront();
// 创建清除缓存请求
const params = {
DistributionId: 'YOUR_DISTRIBUTION_ID', // 替换为你的分配ID
InvalidationBatch: {
CallerReference: Date.now().toString(),
Paths: {
Quantity: 1,
Items: [
'/*' // 清除所有路径的缓存
]
}
}
};
// 发送清除缓存请求
cloudfront.createInvalidation(params, function(err, data) {
if (err) {
console.log(err, err.stack);
} else {
console.log(data);
}
});
方法二:强制更新版本
// 创建AWS CloudFront对象
const cloudfront = new AWS.CloudFront();
// 创建更新分配请求
const params = {
DistributionId: 'YOUR_DISTRIBUTION_ID', // 替换为你的分配ID
IfMatch: 'YOUR_ETAG', // 替换为你的分配的ETag值
DistributionConfig: {
CallerReference: Date.now().toString() // 生成一个唯一的CallerReference值
}
};
// 发送更新分配请求
cloudfront.updateDistribution(params, function(err, data) {
if (err) {
console.log(err, err.stack);
} else {
console.log(data);
}
});
这些代码示例使用 AWS SDK for JavaScript 来与 AWS CloudFront 进行交互。你需要安装并配置 AWS SDK,并替换示例中的占位符(YOUR_DISTRIBUTION_ID、YOUR_ETAG)为你自己的实际值。
请注意,这些操作可能需要一些时间来生效,具体取决于你的分配配置和 AWS CloudFront 服务的状态。