From d6688a367dc8dee7c489790daa5389de08e0531a Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期二, 09 七月 2024 10:42:40 +0800
Subject: [PATCH] !562 ♥️发布 5.2.1 正式版本 Merge pull request !562 from 疯狂的狮子Li/dev

---
 ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java |   90 +++++++++++++++++++++++++++++----------------
 1 files changed, 58 insertions(+), 32 deletions(-)

diff --git a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java
index 6c402dc..5e300da 100644
--- a/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java
+++ b/ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java
@@ -14,11 +14,14 @@
 import org.dromara.common.oss.properties.OssProperties;
 import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
 import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
+import software.amazon.awssdk.core.ResponseInputStream;
 import software.amazon.awssdk.core.async.AsyncRequestBody;
+import software.amazon.awssdk.core.async.AsyncResponseTransformer;
 import software.amazon.awssdk.core.async.BlockingInputStreamAsyncRequestBody;
 import software.amazon.awssdk.regions.Region;
 import software.amazon.awssdk.services.s3.S3AsyncClient;
 import software.amazon.awssdk.services.s3.S3Configuration;
+import software.amazon.awssdk.services.s3.model.GetObjectResponse;
 import software.amazon.awssdk.services.s3.model.NoSuchBucketException;
 import software.amazon.awssdk.services.s3.model.S3Exception;
 import software.amazon.awssdk.services.s3.presigner.S3Presigner;
@@ -26,15 +29,11 @@
 import software.amazon.awssdk.transfer.s3.model.*;
 import software.amazon.awssdk.transfer.s3.progress.LoggingTransferListener;
 
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.net.URI;
 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锛孧inIO 浣跨敤 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()
@@ -160,13 +162,14 @@
     /**
      * 涓婁紶鏂囦欢鍒� Amazon S3锛屽苟杩斿洖涓婁紶缁撴灉
      *
-     * @param filePath  鏈湴鏂囦欢璺緞
-     * @param key       鍦� Amazon S3 涓殑瀵硅薄閿�
-     * @param md5Digest 鏈湴鏂囦欢鐨� MD5 鍝堝笇鍊硷紙鍙�夛級
+     * @param filePath    鏈湴鏂囦欢璺緞
+     * @param key         鍦� Amazon S3 涓殑瀵硅薄閿�
+     * @param md5Digest   鏈湴鏂囦欢鐨� MD5 鍝堝笇鍊硷紙鍙�夛級
+     * @param contentType 鏂囦欢鍐呭绫诲瀷
      * @return UploadResult 鍖呭惈涓婁紶鍚庣殑鏂囦欢淇℃伅
      * @throws OssException 濡傛灉涓婁紶澶辫触锛屾姏鍑鸿嚜瀹氫箟寮傚父
      */
-    public UploadResult upload(Path filePath, String key, String md5Digest) {
+    public UploadResult upload(Path filePath, String key, String md5Digest, String contentType) {
         try {
             // 鏋勫缓涓婁紶璇锋眰瀵硅薄
             FileUpload fileUpload = transferManager.uploadFile(
@@ -174,6 +177,8 @@
                         y -> y.bucket(properties.getBucketName())
                             .key(key)
                             .contentMD5(StringUtils.isNotEmpty(md5Digest) ? md5Digest : null)
+                            .contentType(contentType)
+                            .acl(getAccessPolicy().getObjectCannedACL())
                             .build())
                     .addTransferListener(LoggingTransferListener.create())
                     .source(filePath).build());
@@ -199,10 +204,11 @@
      * @param inputStream 瑕佷笂浼犵殑杈撳叆娴�
      * @param key         鍦� Amazon S3 涓殑瀵硅薄閿�
      * @param length      杈撳叆娴佺殑闀垮害
+     * @param contentType 鏂囦欢鍐呭绫诲瀷
      * @return UploadResult 鍖呭惈涓婁紶鍚庣殑鏂囦欢淇℃伅
      * @throws OssException 濡傛灉涓婁紶澶辫触锛屾姏鍑鸿嚜瀹氫箟寮傚父
      */
-    public UploadResult upload(InputStream inputStream, String key, Long length) {
+    public UploadResult upload(InputStream inputStream, String key, Long length, String contentType) {
         // 濡傛灉杈撳叆娴佷笉鏄� ByteArrayInputStream锛屽垯灏嗗叾璇诲彇涓哄瓧鑺傛暟缁勫啀鍒涘缓 ByteArrayInputStream
         if (!(inputStream instanceof ByteArrayInputStream)) {
             inputStream = new ByteArrayInputStream(IoUtil.readBytes(inputStream));
@@ -217,6 +223,8 @@
                     .putObjectRequest(
                         y -> y.bucket(properties.getBucketName())
                             .key(key)
+                            .contentType(contentType)
+                            .acl(getAccessPolicy().getObjectCannedACL())
                             .build())
                     .build());
 
@@ -242,16 +250,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)
@@ -259,6 +264,37 @@
         // 绛夊緟鏂囦欢涓嬭浇鎿嶄綔瀹屾垚
         downloadFile.completionFuture().join();
         return tempFilePath;
+    }
+
+    /**
+     * 涓嬭浇鏂囦欢浠� Amazon S3 鍒� 杈撳嚭娴�
+     *
+     * @param key 鏂囦欢鍦� Amazon S3 涓殑瀵硅薄閿�
+     * @param out 杈撳嚭娴�
+     * @return 杈撳嚭娴佷腑鍐欏叆鐨勫瓧鑺傛暟锛堥暱搴︼級
+     * @throws OssException 濡傛灉涓嬭浇澶辫触锛屾姏鍑鸿嚜瀹氫箟寮傚父
+     */
+    public long download(String key, OutputStream out) {
+        try {
+            // 鏋勫缓涓嬭浇璇锋眰
+            DownloadRequest<ResponseInputStream<GetObjectResponse>> downloadRequest = DownloadRequest.builder()
+                // 鏂囦欢瀵硅薄
+                .getObjectRequest(y -> y.bucket(properties.getBucketName())
+                    .key(key)
+                    .build())
+                .addTransferListener(LoggingTransferListener.create())
+                // 浣跨敤璁㈤槄杞崲鍣�
+                .responseTransformer(AsyncResponseTransformer.toBlockingInputStream())
+                .build();
+            // 浣跨敤 S3TransferManager 涓嬭浇鏂囦欢
+            Download<ResponseInputStream<GetObjectResponse>> responseFuture = transferManager.download(downloadRequest);
+            // 杈撳嚭鍒版祦涓�
+            try (ResponseInputStream<GetObjectResponse> responseStream = responseFuture.completionFuture().join().result()) { // auto-closeable stream
+                return responseStream.transferTo(out); // 闃诲璋冪敤绾跨▼ blocks the calling thread
+            }
+        } catch (Exception e) {
+            throw new OssException("鏂囦欢涓嬭浇澶辫触锛岄敊璇俊鎭�:[" + e.getMessage() + "]");
+        }
     }
 
     /**
@@ -305,7 +341,7 @@
      * @throws OssException 濡傛灉涓婁紶澶辫触锛屾姏鍑鸿嚜瀹氫箟寮傚父
      */
     public UploadResult uploadSuffix(byte[] data, String suffix) {
-        return upload(new ByteArrayInputStream(data), getPath(properties.getPrefix(), suffix), Long.valueOf(data.length));
+        return upload(new ByteArrayInputStream(data), getPath(properties.getPrefix(), suffix), Long.valueOf(data.length), FileUtils.getMimeType(suffix));
     }
 
     /**
@@ -318,7 +354,7 @@
      * @throws OssException 濡傛灉涓婁紶澶辫触锛屾姏鍑鸿嚜瀹氫箟寮傚父
      */
     public UploadResult uploadSuffix(InputStream inputStream, String suffix, Long length) {
-        return upload(inputStream, getPath(properties.getPrefix(), suffix), length);
+        return upload(inputStream, getPath(properties.getPrefix(), suffix), length, FileUtils.getMimeType(suffix));
     }
 
     /**
@@ -330,7 +366,7 @@
      * @throws OssException 濡傛灉涓婁紶澶辫触锛屾姏鍑鸿嚜瀹氫箟寮傚父
      */
     public UploadResult uploadSuffix(File file, String suffix) {
-        return upload(file.toPath(), getPath(properties.getPrefix(), suffix), null);
+        return upload(file.toPath(), getPath(properties.getPrefix(), suffix), null, FileUtils.getMimeType(suffix));
     }
 
     /**
@@ -449,16 +485,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();
     }
 
     /**

--
Gitblit v1.9.3