AprilWind
2024-04-29 155aa3ba5ca5933fae9fa260c9abb85034b0f31b
ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java
@@ -34,7 +34,6 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
/**
@@ -84,6 +83,9 @@
            StaticCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(
                AwsBasicCredentials.create(properties.getAccessKey(), properties.getSecretKey()));
            //MinIO 使用 HTTPS 限制使用域名访问,站点填域名。需要启用路径样式访问
            boolean isStyle = !StringUtils.containsAny(properties.getEndpoint(), OssConstant.CLOUD_SERVICE);
            //创建AWS基于 CRT 的 S3 客户端
            this.client = S3AsyncClient.crtBuilder()
                .credentialsProvider(credentialsProvider)
@@ -92,15 +94,15 @@
                .targetThroughputInGbps(20.0)
                .minimumPartSizeInBytes(10 * 1025 * 1024L)
                .checksumValidationEnabled(false)
                .forcePathStyle(isStyle)
                .build();
            //AWS基于 CRT 的 S3 AsyncClient 实例用作 S3 传输管理器的底层客户端
            this.transferManager = S3TransferManager.builder().s3Client(this.client).build();
            // 检查是否连接到 MinIO,MinIO 使用 HTTPS 限制使用域名访问,需要启用路径样式访问
            // 创建 S3 配置对象
            S3Configuration config = S3Configuration.builder().chunkedEncodingEnabled(false)
                // minio 使用https限制使用域名访问 需要此配置 站点填域名
                .pathStyleAccessEnabled(!StringUtils.containsAny(properties.getEndpoint(), OssConstant.CLOUD_SERVICE)).build();
                .pathStyleAccessEnabled(isStyle).build();
            // 创建 预签名 URL 的生成器 实例,用于生成 S3 预签名 URL
            this.presigner = S3Presigner.builder()
@@ -242,16 +244,13 @@
     * @throws OssException 如果下载失败,抛出自定义异常
     */
    public Path fileDownload(String path) {
        // 从路径中移除 URL 前缀
        String url = removeBaseUrl(path);
        // 构建临时文件路径 文件名必须是唯一不存在的,路径必须是存在的
        Path tempFilePath = Paths.get(extractFileName(url));
        // 构建临时文件
        Path tempFilePath = FileUtils.createTempFile().toPath();
        // 使用 S3TransferManager 下载文件
        FileDownload downloadFile = transferManager.downloadFile(
            x -> x.getObjectRequest(
                    y -> y.bucket(properties.getBucketName())
                        .key(url)
                        .key(removeBaseUrl(path))
                        .build())
                .addTransferListener(LoggingTransferListener.create())
                .destination(tempFilePath)
@@ -449,16 +448,6 @@
     */
    public String removeBaseUrl(String path) {
        return path.replace(getUrl() + StringUtils.SLASH, "");
    }
    /**
     * 从文件路径中提取文件名
     *
     * @param path 文件路径
     * @return 提取的文件名或默认文件名
     */
    public String extractFileName(String path) {
        return FileUtils.getTmpDir() + StringUtils.SLASH + Paths.get(path).getFileName().toString();
    }
    /**