From 2d8ded3e77b22e44985265ca4063102662e452c1 Mon Sep 17 00:00:00 2001
From: sean.zhou <sean.zhou@dji.com>
Date: Mon, 12 Dec 2022 18:32:19 +0800
Subject: [PATCH] initial v1.3.1

---
 src/main/java/com/dji/sample/component/oss/service/impl/MinIOServiceImpl.java |   38 ++++++++++++++------------------------
 1 files changed, 14 insertions(+), 24 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 b162751..cafeb66 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
@@ -9,7 +9,6 @@
 import io.minio.errors.*;
 import io.minio.http.Method;
 import lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.io.ByteArrayInputStream;
@@ -30,10 +29,7 @@
 public class MinIOServiceImpl implements IOssService {
 
     private MinioClient client;
-
-    @Autowired
-    private OssConfiguration configuration;
-
+    
     @Override
     public String getOssType() {
         return OssTypeEnum.MINIO.getType();
@@ -42,10 +38,10 @@
     @Override
     public CredentialsDTO getCredentials() {
         try {
-            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(), configuration.getExpire());
+            AssumeRoleProvider provider = new AssumeRoleProvider(OssConfiguration.endpoint, OssConfiguration.accessKey,
+                    OssConfiguration.secretKey, Math.toIntExact(OssConfiguration.expire),
+                    null, OssConfiguration.region, null, null, null, null);
+            return new CredentialsDTO(provider.fetch(), OssConfiguration.expire);
         } catch (NoSuchAlgorithmException e) {
             log.debug("Failed to obtain sts.");
             e.printStackTrace();
@@ -57,26 +53,22 @@
     public URL getObjectUrl(String bucket, String objectKey) {
         try {
             return new URL(
-                    this.createClient()
-                            .getPresignedObjectUrl(
+                    client.getPresignedObjectUrl(
                                     GetPresignedObjectUrlArgs.builder()
                                             .method(Method.GET)
                                             .bucket(bucket)
                                             .object(objectKey)
-                                            .expiry(Math.toIntExact(configuration.getExpire()))
+                                            .expiry(Math.toIntExact(OssConfiguration.expire))
                                             .build()));
         } catch (ErrorResponseException | InsufficientDataException | InternalException |
                 InvalidKeyException | InvalidResponseException | IOException |
                 NoSuchAlgorithmException | XmlParserException | ServerException e) {
-            log.error("The file does not exist on the OssConfiguration.");
-            e.printStackTrace();
+            throw new RuntimeException("The file does not exist on the OssConfiguration.");
         }
-        return null;
     }
 
     @Override
     public Boolean deleteObject(String bucket, String objectKey) {
-        MinioClient client = this.createClient();
         try {
             client.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectKey).build());
         } catch (MinioException | NoSuchAlgorithmException | IOException | InvalidKeyException e) {
@@ -90,7 +82,7 @@
     @Override
     public InputStream getObject(String bucket, String objectKey) {
         try {
-            GetObjectResponse object = this.createClient().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();
@@ -101,7 +93,6 @@
     @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) {
@@ -117,15 +108,14 @@
         }
     }
 
-    private MinioClient createClient() {
+    public void createClient() {
         if (Objects.nonNull(this.client)) {
-            return this.client;
+            return;
         }
         this.client = MinioClient.builder()
-                .endpoint(configuration.getEndpoint())
-                .credentials(configuration.getAccessKey(), configuration.getSecretKey())
-                .region(configuration.getRegion())
+                .endpoint(OssConfiguration.endpoint)
+                .credentials(OssConfiguration.accessKey, OssConfiguration.secretKey)
+                .region(OssConfiguration.region)
                 .build();
-        return this.client;
     }
 }

--
Gitblit v1.9.3