From 6a3a27026d6a7875e45726ed1b727bd1a6fac7e0 Mon Sep 17 00:00:00 2001
From: 疯狂的狮子Li <15040126243@163.com>
Date: 星期一, 20 五月 2024 10:21:07 +0800
Subject: [PATCH] ♥️发布 5.2.0-BETA 公测版本
---
ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java | 47 +++++++++++++++++++++++++++++++++++++++--------
1 files changed, 39 insertions(+), 8 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 b8f84f8..fcad35b 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,10 +29,7 @@
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;
@@ -84,7 +84,7 @@
AwsBasicCredentials.create(properties.getAccessKey(), properties.getSecretKey()));
//MinIO 浣跨敤 HTTPS 闄愬埗浣跨敤鍩熷悕璁块棶锛岀珯鐐瑰~鍩熷悕銆傞渶瑕佸惎鐢ㄨ矾寰勬牱寮忚闂�
- boolean pathStyle = !StringUtils.containsAny(properties.getEndpoint(), OssConstant.CLOUD_SERVICE);
+ boolean isStyle = !StringUtils.containsAny(properties.getEndpoint(), OssConstant.CLOUD_SERVICE);
//鍒涘缓AWS鍩轰簬 CRT 鐨� S3 瀹㈡埛绔�
this.client = S3AsyncClient.crtBuilder()
@@ -94,15 +94,15 @@
.targetThroughputInGbps(20.0)
.minimumPartSizeInBytes(10 * 1025 * 1024L)
.checksumValidationEnabled(false)
- .forcePathStyle(pathStyle)
+ .forcePathStyle(isStyle)
.build();
//AWS鍩轰簬 CRT 鐨� S3 AsyncClient 瀹炰緥鐢ㄤ綔 S3 浼犺緭绠$悊鍣ㄧ殑搴曞眰瀹㈡埛绔�
this.transferManager = S3TransferManager.builder().s3Client(this.client).build();
- // 妫�鏌ユ槸鍚﹁繛鎺ュ埌 MinIO
+ // 鍒涘缓 S3 閰嶇疆瀵硅薄
S3Configuration config = S3Configuration.builder().chunkedEncodingEnabled(false)
- .pathStyleAccessEnabled(pathStyle).build();
+ .pathStyleAccessEnabled(isStyle).build();
// 鍒涘缓 棰勭鍚� URL 鐨勭敓鎴愬櫒 瀹炰緥锛岀敤浜庣敓鎴� S3 棰勭鍚� URL
this.presigner = S3Presigner.builder()
@@ -261,6 +261,37 @@
}
/**
+ * 涓嬭浇鏂囦欢浠� 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() + "]");
+ }
+ }
+
+ /**
* 鍒犻櫎浜戝瓨鍌ㄦ湇鍔′腑鎸囧畾璺緞涓嬫枃浠�
*
* @param path 鎸囧畾璺緞
--
Gitblit v1.9.3