From e4aeeaf6fc665faa0f1e9e71c60609d62a9b093f Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Fri, 21 Nov 2025 17:00:49 +0800
Subject: [PATCH] 修复机场挂载相机无法删除的问题

---
 ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java |   66 ++++++++++++++++++++++++++++++---
 1 files changed, 60 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 5097bce..e9b3f96 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
@@ -46,7 +46,7 @@
     }
 
 
-    public CredentialsDTO getCredentials1111() {
+    public CredentialsDTO getCredentials111() {
         try {
             AssumeRoleProvider provider = new AssumeRoleProvider(endpoint, accessKey,
                     secretKey, Math.toIntExact(OssConfiguration.expire),
@@ -72,7 +72,7 @@
                         public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[]{}; }
                     }
             };
-            SSLContext sslContext = SSLContext.getInstance("TLS");
+            SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
             sslContext.init(null, trustAllCerts, new SecureRandom());
             OkHttpClient httpClient = new OkHttpClient.Builder()
                     .sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0])
@@ -127,7 +127,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();
@@ -139,7 +142,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();
@@ -150,7 +156,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.");
@@ -158,7 +167,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);
@@ -166,6 +179,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)) {

--
Gitblit v1.9.3