fix: 修复 CDN 刷新静默失败问题
- 添加 CDN 刷新日志输出,便于排查问题 - 添加密钥配置检查,未配置时给出明确提示 - 修复生产环境日志不输出的问题 - CDN 刷新失败不影响上传结果,仅记录警告
This commit is contained in:
@@ -153,25 +153,51 @@ class QiniuUploader {
|
||||
}
|
||||
|
||||
// 刷新 CDN
|
||||
async refreshCDN(bucketName, key) {
|
||||
async refreshCDN(bucketName, key, logCallback) {
|
||||
const bucketConfig = this.config.buckets[bucketName];
|
||||
if (!bucketConfig) return;
|
||||
if (!bucketConfig) {
|
||||
if (logCallback) logCallback(`[CDN 刷新] 存储桶 "${bucketName}" 配置不存在`);
|
||||
return;
|
||||
}
|
||||
|
||||
const { accessKey, secretKey, domain } = bucketConfig;
|
||||
|
||||
// 检查密钥是否有效
|
||||
if (!accessKey || !secretKey || accessKey === 'YOUR_ACCESS_KEY' || secretKey === 'YOUR_SECRET_KEY') {
|
||||
if (logCallback) logCallback(`[CDN 刷新] 警告:存储桶 "${bucketName}" 的密钥未配置,跳过 CDN 刷新`);
|
||||
return;
|
||||
}
|
||||
|
||||
const fileUrl = `${domain}/${key}`;
|
||||
if (logCallback) logCallback(`[CDN 刷新] 请求刷新 URL: ${fileUrl}`);
|
||||
|
||||
const body = JSON.stringify({ urls: [fileUrl] });
|
||||
const accessToken = this.generateAccessToken(accessKey, secretKey, 'POST', '/v2/tune/refresh', body);
|
||||
|
||||
await this.httpRequest('https://fusion.qiniuapi.com/v2/tune/refresh', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Host': 'fusion.qiniuapi.com',
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': body.length,
|
||||
'Authorization': accessToken
|
||||
try {
|
||||
const result = await this.httpRequest('https://fusion.qiniuapi.com/v2/tune/refresh', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Host': 'fusion.qiniuapi.com',
|
||||
'Content-Type': 'application/json',
|
||||
'Content-Length': body.length,
|
||||
'Authorization': accessToken
|
||||
}
|
||||
}, body);
|
||||
|
||||
if (logCallback) logCallback(`[CDN 刷新] 响应状态:${result.status}, 数据:${JSON.stringify(result.data)}`);
|
||||
|
||||
if (result.status !== 200) {
|
||||
throw new Error(`CDN 刷新失败:${JSON.stringify(result.data)}`);
|
||||
}
|
||||
}, body);
|
||||
|
||||
if (logCallback) logCallback(`[CDN 刷新] ✅ 刷新成功`);
|
||||
|
||||
return result.data;
|
||||
} catch (error) {
|
||||
if (logCallback) logCallback(`[CDN 刷新] ❌ 错误:${error.message}`);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
// 生成上传凭证
|
||||
|
||||
Reference in New Issue
Block a user