From 473b5351fbbfd4e5fa4b0f87356253776bfa6da6 Mon Sep 17 00:00:00 2001
From: zhaowenyu <35589726@qq.com>
Date: Mon, 02 Mar 2026 08:25:41 +0800
Subject: [PATCH] 2026-03-02 优化媒体文件查询列表,新增任务名称

---
 ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java |  133 ++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 127 insertions(+), 6 deletions(-)

diff --git a/ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java b/ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java
index 758b8b3..2fe171d 100644
--- a/ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java
+++ b/ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java
@@ -9,7 +9,10 @@
 import io.minio.errors.*;
 import io.minio.http.Method;
 import lombok.extern.slf4j.Slf4j;
+import okhttp3.ConnectionPool;
+import okhttp3.ConnectionSpec;
 import okhttp3.OkHttpClient;
+import okhttp3.TlsVersion;
 import org.springframework.stereotype.Service;
 
 import javax.net.ssl.SSLContext;
@@ -22,9 +25,12 @@
 import java.net.URL;
 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
+import java.security.ProviderException;
 import java.security.SecureRandom;
 import java.security.cert.X509Certificate;
+import java.util.Collections;
 import java.util.Objects;
+import java.util.concurrent.TimeUnit;
 
 import static com.ard.common.config.OssConfiguration.*;
 
@@ -44,8 +50,8 @@
         return OssTypeEnum.MINIO.getType();
     }
 
-    @Override
-    public CredentialsDTO getCredentials() {
+
+    public CredentialsDTO getCredentials111() {
         try {
             AssumeRoleProvider provider = new AssumeRoleProvider(endpoint, accessKey,
                     secretKey, Math.toIntExact(OssConfiguration.expire),
@@ -56,6 +62,62 @@
             e.printStackTrace();
         }
         return null;
+    }
+    @Override
+    public CredentialsDTO getCredentials() {
+        try {
+            // 创建自签证书的 OkHttpClient
+            TrustManager[] trustAllCerts = new TrustManager[]{
+                    new X509TrustManager() {
+                        @Override
+                        public void checkClientTrusted(X509Certificate[] chain, String authType) {}
+                        @Override
+                        public void checkServerTrusted(X509Certificate[] chain, String authType) {}
+                        @Override
+                        public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; }
+                    }
+            };
+            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
+            sslContext.init(null, trustAllCerts, new SecureRandom());
+
+            ConnectionSpec spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
+                    .tlsVersions(TlsVersion.TLS_1_2)
+                    .allEnabledCipherSuites()
+                    .build();
+
+            OkHttpClient httpClient = new OkHttpClient.Builder()
+                    .sslSocketFactory(sslContext.getSocketFactory(),
+                            (X509TrustManager) trustAllCerts[0])
+                    .hostnameVerifier((hostname, session) -> true)
+                    .connectionSpecs(Collections.singletonList(spec)) // ⭐关键
+                    .connectionPool(new ConnectionPool(0, 1, TimeUnit.SECONDS)) // ⭐禁用复用
+                    .build();
+
+            // 构建 AssumeRoleProvider,使用自定义 httpClient
+            AssumeRoleProvider provider = new AssumeRoleProvider(
+                    endpoint,
+                    accessKey,
+                    secretKey,
+                    Math.toIntExact(OssConfiguration.expire),
+                    null,
+                    OssConfiguration.region,
+                    null,
+                    null,
+                    null,
+                    httpClient
+            );
+
+            // 获取 STS 凭证
+            var stsCredentials = provider.fetch();
+            return new CredentialsDTO(stsCredentials, OssConfiguration.expire);
+
+        } catch (ProviderException e) {
+            log.error("Failed to parse STS response", e);
+            throw new RuntimeException("STS response parse failed", e);
+        } catch (NoSuchAlgorithmException | java.security.KeyManagementException e) {
+            log.error("Failed to create custom SSL context", e);
+            throw new RuntimeException("SSL setup failed", e);
+        }
     }
 
     @Override
@@ -79,7 +141,10 @@
     @Override
     public Boolean deleteObject(String bucket, String objectKey) {
         try {
-            client.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectKey).build());
+            client.removeObject(RemoveObjectArgs.builder()
+                    .bucket(bucket)
+                    .object(objectKey)
+                    .build());
         } catch (MinioException | NoSuchAlgorithmException | IOException | InvalidKeyException e) {
             log.error("Failed to delete file.");
             e.printStackTrace();
@@ -91,7 +156,10 @@
     @Override
     public InputStream getObject(String bucket, String objectKey) {
         try {
-            GetObjectResponse object = client.getObject(GetObjectArgs.builder().bucket(bucket).object(objectKey).build());
+            GetObjectResponse object = client.getObject(GetObjectArgs.builder()
+                    .bucket(bucket)
+                    .object(objectKey)
+                    .build());
             return new ByteArrayInputStream(object.readAllBytes());
         } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidKeyException | InvalidResponseException | IOException | NoSuchAlgorithmException | ServerException | XmlParserException e) {
             e.printStackTrace();
@@ -102,7 +170,10 @@
     @Override
     public void putObject(String bucket, String objectKey, InputStream input) {
         try {
-            client.statObject(StatObjectArgs.builder().bucket(bucket).object(objectKey).build());
+            client.statObject(StatObjectArgs.builder()
+                    .bucket(bucket)
+                    .object(objectKey)
+                    .build());
             throw new RuntimeException("The filename already exists.");
         } catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) {
             log.info("The file does not exist, start uploading.");
@@ -110,7 +181,11 @@
                 // 设置 partSize 为 10MB
                 long partSize = 10 * 1024 * 1024;
                 ObjectWriteResponse response = client.putObject(
-                        PutObjectArgs.builder().bucket(bucket).object(objectKey).stream(input, -1, partSize).build());
+                        PutObjectArgs.builder()
+                                .bucket(bucket)
+                                .object(objectKey)
+                                .stream(input, -1, partSize)
+                                .build());
                 log.info("Upload File: {}", response.etag());
             } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException ex) {
                 log.error("Failed to upload File {}.", objectKey);
@@ -118,6 +193,47 @@
             }
         }
     }
+
+    @Override
+    public void putObject(String bucket, String objectKey, InputStream input, String contentType) {
+        try {
+            // 检查文件是否已经存在
+            client.statObject(StatObjectArgs.builder()
+                    .bucket(bucket)
+                    .object(objectKey)
+                    .build());
+            throw new RuntimeException("The file already exists: " + objectKey);
+        } catch (ErrorResponseException e) {
+            // 如果文件不存在,则继续上传
+            log.info("The file does not exist, start uploading.");
+            try {
+                // 设置 partSize 为 10MB
+                long partSize = 10 * 1024 * 1024; // 每块 10MB
+                PutObjectArgs.Builder putArgsBuilder = PutObjectArgs.builder()
+                        .bucket(bucket)
+                        .object(objectKey)
+                        .stream(input, -1, partSize);
+
+                // 如果提供了 Content-Type,则设置该类型
+                if (contentType != null && !contentType.isEmpty()) {
+                    putArgsBuilder.contentType(contentType);
+                }
+
+                // 上传文件
+                ObjectWriteResponse response = client.putObject(putArgsBuilder.build());
+                log.info("Uploaded file successfully: {}, ETag: {}", objectKey, response.etag());
+
+            } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException ex) {
+                log.error("Failed to upload file: {}", objectKey, ex);
+                throw new RuntimeException("File upload failed: " + objectKey, ex);
+            }
+        } catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) {
+            // 如果 statObject 发生错误,说明服务端错误或其他问题
+            log.error("Failed to check if file exists: {}", objectKey, e);
+            throw new RuntimeException("Failed to check if file exists: " + objectKey, e);
+        }
+    }
+
     @Override
     public void createClient() {
         if (Objects.nonNull(this.client)) {
@@ -142,6 +258,11 @@
             OkHttpClient httpClient = new OkHttpClient.Builder()
                     .sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0])
                     .hostnameVerifier((hostname, session) -> true)
+                    .connectionSpecs(Collections.singletonList(
+                            new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
+                                    .tlsVersions(TlsVersion.TLS_1_2)   // 强制 TLS1.2
+                                    .build()
+                    ))
                     .build();
 
             this.client = MinioClient.builder()

--
Gitblit v1.9.3