From 56df98ce4952239fbf7d0e99dbeb0e5c71531d6f Mon Sep 17 00:00:00 2001
From: sean.zhou <sean.zhou@dji.com>
Date: Fri, 18 Nov 2022 18:29:06 +0800
Subject: [PATCH] initial v1.3.0

---
 src/main/java/com/dji/sample/component/oss/service/impl/MinIOServiceImpl.java |   55 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/src/main/java/com/dji/sample/component/oss/service/impl/MinIOServiceImpl.java b/src/main/java/com/dji/sample/component/oss/service/impl/MinIOServiceImpl.java
index 633c35f..b162751 100644
--- a/src/main/java/com/dji/sample/component/oss/service/impl/MinIOServiceImpl.java
+++ b/src/main/java/com/dji/sample/component/oss/service/impl/MinIOServiceImpl.java
@@ -4,8 +4,7 @@
 import com.dji.sample.component.oss.model.enums.OssTypeEnum;
 import com.dji.sample.component.oss.service.IOssService;
 import com.dji.sample.media.model.CredentialsDTO;
-import io.minio.GetPresignedObjectUrlArgs;
-import io.minio.MinioClient;
+import io.minio.*;
 import io.minio.credentials.AssumeRoleProvider;
 import io.minio.errors.*;
 import io.minio.http.Method;
@@ -13,10 +12,13 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URL;
 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
+import java.util.Objects;
 
 /**
  * @author sean
@@ -26,6 +28,8 @@
 @Service
 @Slf4j
 public class MinIOServiceImpl implements IOssService {
+
+    private MinioClient client;
 
     @Autowired
     private OssConfiguration configuration;
@@ -41,7 +45,7 @@
             AssumeRoleProvider provider = new AssumeRoleProvider(configuration.getEndpoint(), configuration.getAccessKey(),
                     configuration.getSecretKey(), Math.toIntExact(configuration.getExpire()),
                     null, configuration.getRegion(), null, null, null, null);
-            return new CredentialsDTO(provider.fetch(), Math.toIntExact(configuration.getExpire()));
+            return new CredentialsDTO(provider.fetch(), configuration.getExpire());
         } catch (NoSuchAlgorithmException e) {
             log.debug("Failed to obtain sts.");
             e.printStackTrace();
@@ -72,19 +76,56 @@
 
     @Override
     public Boolean deleteObject(String bucket, String objectKey) {
-        return null;
+        MinioClient client = this.createClient();
+        try {
+            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;
+        }
+        return true;
     }
 
     @Override
-    public byte[] getObject(String bucket, String objectKey) {
-        return new byte[0];
+    public InputStream getObject(String bucket, String objectKey) {
+        try {
+            GetObjectResponse object = this.createClient().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();
+        }
+        return InputStream.nullInputStream();
+    }
+
+    @Override
+    public void putObject(String bucket, String objectKey, InputStream input) {
+        try {
+            MinioClient client = this.createClient();
+            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.");
+            try {
+                ObjectWriteResponse response = client.putObject(
+                        PutObjectArgs.builder().bucket(bucket).object(objectKey).stream(input, input.available(), 0).build());
+                log.info("Upload File: {}", response.etag());
+            } catch (MinioException | IOException | InvalidKeyException | NoSuchAlgorithmException ex) {
+                log.error("Failed to upload File {}.", objectKey);
+                ex.printStackTrace();
+            }
+        }
     }
 
     private MinioClient createClient() {
-        return MinioClient.builder()
+        if (Objects.nonNull(this.client)) {
+            return this.client;
+        }
+        this.client = MinioClient.builder()
                 .endpoint(configuration.getEndpoint())
                 .credentials(configuration.getAccessKey(), configuration.getSecretKey())
                 .region(configuration.getRegion())
                 .build();
+        return this.client;
     }
 }

--
Gitblit v1.9.3