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