update 优化 oss 下载使用临时文件避免线程冲突

This commit is contained in:
疯狂的狮子Li 2024-01-15 09:36:55 +08:00
parent e115f5f2f4
commit 13e60a6048

View File

@ -34,7 +34,6 @@ import java.net.URI;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration; import java.time.Duration;
/** /**
@ -242,16 +241,13 @@ public class OssClient {
* @throws OssException 如果下载失败抛出自定义异常 * @throws OssException 如果下载失败抛出自定义异常
*/ */
public Path fileDownload(String path) { public Path fileDownload(String path) {
// 从路径中移除 URL 前缀 // 构建临时文件
String url = removeBaseUrl(path); Path tempFilePath = FileUtils.createTempFile().toPath();
// 构建临时文件路径 文件名必须是唯一不存在的路径必须是存在的
Path tempFilePath = Paths.get(extractFileName(url));
// 使用 S3TransferManager 下载文件 // 使用 S3TransferManager 下载文件
FileDownload downloadFile = transferManager.downloadFile( FileDownload downloadFile = transferManager.downloadFile(
x -> x.getObjectRequest( x -> x.getObjectRequest(
y -> y.bucket(properties.getBucketName()) y -> y.bucket(properties.getBucketName())
.key(url) .key(removeBaseUrl(path))
.build()) .build())
.addTransferListener(LoggingTransferListener.create()) .addTransferListener(LoggingTransferListener.create())
.destination(tempFilePath) .destination(tempFilePath)
@ -451,16 +447,6 @@ public class OssClient {
return path.replace(getUrl() + StringUtils.SLASH, ""); return path.replace(getUrl() + StringUtils.SLASH, "");
} }
/**
* 从文件路径中提取文件名
*
* @param path 文件路径
* @return 提取的文件名或默认文件名
*/
public String extractFileName(String path) {
return FileUtils.getTmpDir() + StringUtils.SLASH + Paths.get(path).getFileName().toString();
}
/** /**
* 服务商 * 服务商
*/ */