fix: Qiniu 读取文件 CDN 404 时回退源站直读
This commit is contained in:
@@ -70,15 +70,27 @@ public class QiniuStorageService implements StorageService {
|
|||||||
@Override
|
@Override
|
||||||
public String read(String path) {
|
public String read(String path) {
|
||||||
ensureInit();
|
ensureInit();
|
||||||
|
// 先走 CDN,404 则回退源站直读
|
||||||
|
String content = readFromUrl(cdnDomain + "/" + path, path);
|
||||||
|
if (content != null) return content;
|
||||||
|
// CDN 缓存未命中,走 Qiniu 源站
|
||||||
|
String sourceUrl = "https://" + bucket + ".qiniucdn.com/" + path;
|
||||||
|
String privateUrl = auth.privateDownloadUrl(sourceUrl, 3600);
|
||||||
|
content = readFromUrl(privateUrl, path);
|
||||||
|
if (content != null) return content;
|
||||||
|
log.warn("Qiniu 读取失败 {}: CDN+源站均不可用", path);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String readFromUrl(String url, String path) {
|
||||||
try {
|
try {
|
||||||
String url = cdnDomain + "/" + path;
|
|
||||||
var req = HttpRequest.newBuilder().uri(URI.create(url))
|
var req = HttpRequest.newBuilder().uri(URI.create(url))
|
||||||
.timeout(Duration.ofSeconds(10)).GET().build();
|
.timeout(Duration.ofSeconds(10)).GET().build();
|
||||||
var resp = httpClient.send(req, HttpResponse.BodyHandlers.ofString());
|
var resp = httpClient.send(req, HttpResponse.BodyHandlers.ofString());
|
||||||
if (resp.statusCode() == 200) return resp.body();
|
if (resp.statusCode() == 200) return resp.body();
|
||||||
log.warn("Qiniu 读取失败 {}: HTTP {}", path, resp.statusCode());
|
log.debug("Qiniu CDN miss {}: HTTP {}", path, resp.statusCode());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Qiniu 读取失败 {}: {}", path, e.getMessage());
|
log.debug("Qiniu 读取异常 {}: {}", path, e.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user