From 9f911c1461ba33c1a561046eb1ac8e1c56fc9b9d Mon Sep 17 00:00:00 2001
From: liusuyi <1951119284@qq.com>
Date: Thu, 30 Oct 2025 11:47:20 +0800
Subject: [PATCH] 实体对象基类增加是否使用数据范围

---
 ard-oss/src/main/java/com/ard/oss/service/impl/MinIOServiceImpl.java |   62 +++++++++++++++++++++----------
 1 files changed, 42 insertions(+), 20 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 e38cfb0..38718a3 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
@@ -127,28 +127,13 @@
     @Override
     public Boolean deleteObject(String bucket, String objectKey) {
         try {
-            // 检查文件是否存在
-            client.statObject(StatObjectArgs.builder()
-                    .bucket(bucket)
-                    .object(objectKey)
-                    .build());
-
-            // 文件存在,执行删除
-            client.removeObject(RemoveObjectArgs.builder()
-                    .bucket(bucket)
-                    .object(objectKey)
-                    .build());
-
-            log.info("成功删除文件: {}/{}", bucket, objectKey);
-            return true;
-        } catch (ErrorResponseException e) {
-            // 文件不存在的情况
-            log.warn("文件不存在: {}/{}", bucket, objectKey);
+            client.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectKey).build());
+        } catch (MinioException | NoSuchAlgorithmException | IOException | InvalidKeyException e) {
+            log.error("Failed to delete file.");
+            e.printStackTrace();
             return false;
-        } catch (Exception e) {
-            log.error("删除文件失败: {}/{}", bucket, objectKey, e);
-            throw new RuntimeException("删除文件失败: " + e.getMessage(), e);
         }
+        return true;
     }
 
     @Override
@@ -182,6 +167,43 @@
         }
     }
     @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)) {
             return;

--
Gitblit v1.9.3