fix: Qiniu 下载改用标准 HttpClient,日志输出完整签名URL
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package com.par.core.storage;
|
||||
|
||||
import com.qiniu.common.QiniuException;
|
||||
import com.qiniu.http.Client;
|
||||
import com.qiniu.http.Response;
|
||||
import com.qiniu.storage.BucketManager;
|
||||
import com.qiniu.storage.Configuration;
|
||||
import com.qiniu.storage.DownloadUrl;
|
||||
@@ -12,6 +10,12 @@ import com.qiniu.util.Auth;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* 七牛云存储。
|
||||
* 上传使用 UploadManager,下载使用 DownloadUrl + Auth 签名。
|
||||
@@ -22,7 +26,7 @@ public class QiniuStorageService implements StorageService {
|
||||
|
||||
private UploadManager uploadManager;
|
||||
private BucketManager bucketManager;
|
||||
private Client qiniuClient;
|
||||
private HttpClient httpClient;
|
||||
private Auth auth;
|
||||
private String bucket;
|
||||
private String downloadDomain;
|
||||
@@ -57,7 +61,10 @@ public class QiniuStorageService implements StorageService {
|
||||
Configuration cfg = new Configuration(region);
|
||||
this.uploadManager = new UploadManager(cfg);
|
||||
this.bucketManager = new BucketManager(auth, cfg);
|
||||
this.qiniuClient = new Client(cfg);
|
||||
this.httpClient = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(5))
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.build();
|
||||
log.info("Qiniu 存储就绪: bucket={}, domain={}", bucket, downloadDomain);
|
||||
}
|
||||
|
||||
@@ -86,16 +93,18 @@ public class QiniuStorageService implements StorageService {
|
||||
log.warn("Qiniu stat 失败 {}: {}", path, e.response != null ? e.response.error : e.getMessage());
|
||||
}
|
||||
|
||||
// 用 DownloadUrl 生成带签名的下载链接,SDK Client 下载
|
||||
// 用 DownloadUrl 生成带签名的下载链接
|
||||
long deadline = System.currentTimeMillis() / 1000 + 3600;
|
||||
try {
|
||||
DownloadUrl url = new DownloadUrl(downloadDomain, useHttps, path);
|
||||
String signedUrl = url.buildURL(auth, deadline);
|
||||
Response resp = qiniuClient.get(signedUrl);
|
||||
if (resp.isOK()) return resp.bodyString();
|
||||
log.warn("Qiniu 下载失败 {}: HTTP {}", path, resp.statusCode);
|
||||
} catch (QiniuException e) {
|
||||
log.warn("Qiniu 下载失败 {}: {}", path, e.response != null ? e.response.error : e.getMessage());
|
||||
var req = HttpRequest.newBuilder().uri(URI.create(signedUrl))
|
||||
.timeout(Duration.ofSeconds(10)).GET().build();
|
||||
var resp = httpClient.send(req, HttpResponse.BodyHandlers.ofString());
|
||||
if (resp.statusCode() == 200) return resp.body();
|
||||
log.warn("Qiniu 下载失败 {}: HTTP {} url={}", path, resp.statusCode(), signedUrl);
|
||||
} catch (Exception e) {
|
||||
log.warn("Qiniu 下载失败 {}: {}", path, e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user