fix: 私有空间直接用认证源站下载,不走 CDN
This commit is contained in:
@@ -70,27 +70,17 @@ 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 sourceUrl = "https://" + bucket + ".qiniucdn.com/" + path;
|
||||||
String privateUrl = auth.privateDownloadUrl(sourceUrl, 3600);
|
String signedUrl = 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 {
|
||||||
var req = HttpRequest.newBuilder().uri(URI.create(url))
|
var req = HttpRequest.newBuilder().uri(URI.create(signedUrl))
|
||||||
.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.debug("Qiniu CDN miss {}: HTTP {}", path, resp.statusCode());
|
log.warn("Qiniu 读取失败 {}: HTTP {}", path, resp.statusCode());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.debug("Qiniu 读取异常 {}: {}", path, e.getMessage());
|
log.warn("Qiniu 读取失败 {}: {}", path, e.getMessage());
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user