From dc5c92ee2d2bf4b4e938a2c0f09541d9caa019fe Mon Sep 17 00:00:00 2001
From: AprilWind <2100166581@qq.com>
Date: 星期三, 16 十月 2024 10:33:11 +0800
Subject: [PATCH] fix 修复同步云厂商要求明确配置访问样式(路径样式访问)

---
 ruoyi-common/ruoyi-common-oss/src/main/java/org/dromara/common/oss/core/OssClient.java |   30 ++++++++++++++++++------------
 1 files changed, 18 insertions(+), 12 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 fcad35b..12e49f3 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
@@ -83,8 +83,8 @@
             StaticCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(
                 AwsBasicCredentials.create(properties.getAccessKey(), properties.getSecretKey()));
 
-            //MinIO 浣跨敤 HTTPS 闄愬埗浣跨敤鍩熷悕璁块棶锛岀珯鐐瑰~鍩熷悕銆傞渶瑕佸惎鐢ㄨ矾寰勬牱寮忚闂�
-            boolean isStyle = !StringUtils.containsAny(properties.getEndpoint(), OssConstant.CLOUD_SERVICE);
+            //浣跨敤瀵硅薄瀛樺偍鏈嶅姟鏃惰姹傛槑纭厤缃闂牱寮忥紙璺緞鏍峰紡鎴栬櫄鎷熸墭绠℃牱寮忥級銆傞渶瑕佸惎鐢ㄨ矾寰勬牱寮忚闂�
+            boolean isStyle = true;
 
             //鍒涘缓AWS鍩轰簬 CRT 鐨� S3 瀹㈡埛绔�
             this.client = S3AsyncClient.crtBuilder()
@@ -162,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(
@@ -176,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());
@@ -201,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));
@@ -219,6 +223,8 @@
                     .putObjectRequest(
                         y -> y.bucket(properties.getBucketName())
                             .key(key)
+                            .contentType(contentType)
+                            .acl(getAccessPolicy().getObjectCannedACL())
                             .build())
                     .build());
 
@@ -334,8 +340,8 @@
      * @return UploadResult 鍖呭惈涓婁紶鍚庣殑鏂囦欢淇℃伅
      * @throws OssException 濡傛灉涓婁紶澶辫触锛屾姏鍑鸿嚜瀹氫箟寮傚父
      */
-    public UploadResult uploadSuffix(byte[] data, String suffix) {
-        return upload(new ByteArrayInputStream(data), getPath(properties.getPrefix(), suffix), Long.valueOf(data.length));
+    public UploadResult uploadSuffix(byte[] data, String suffix, String contentType) {
+        return upload(new ByteArrayInputStream(data), getPath(properties.getPrefix(), suffix), Long.valueOf(data.length), contentType);
     }
 
     /**
@@ -347,8 +353,8 @@
      * @return UploadResult 鍖呭惈涓婁紶鍚庣殑鏂囦欢淇℃伅
      * @throws OssException 濡傛灉涓婁紶澶辫触锛屾姏鍑鸿嚜瀹氫箟寮傚父
      */
-    public UploadResult uploadSuffix(InputStream inputStream, String suffix, Long length) {
-        return upload(inputStream, getPath(properties.getPrefix(), suffix), length);
+    public UploadResult uploadSuffix(InputStream inputStream, String suffix, Long length, String contentType) {
+        return upload(inputStream, getPath(properties.getPrefix(), suffix), length, contentType);
     }
 
     /**
@@ -360,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));
     }
 
     /**

--
Gitblit v1.9.3