From a7aaeabc7873a0eafb4a7ecad7f65b018b7a9bc9 Mon Sep 17 00:00:00 2001
From: sean.zhou <sean.zhou@dji.com>
Date: Fri, 24 Feb 2023 19:31:23 +0800
Subject: [PATCH] What's new? 1. Add license for dock. 2. Modify the logic corresponding to the firmware file and device type. 3. Add multiple mqtt clients options. 4. Modify the structure of the interface for obtaining the device list. 5. Fixed some issues.

---
 src/main/java/com/dji/sample/component/oss/service/impl/AmazonS3ServiceImpl.java |   84 ++++++++++++++++++-----------------------
 1 files changed, 37 insertions(+), 47 deletions(-)

diff --git a/src/main/java/com/dji/sample/component/oss/service/impl/AmazonS3ServiceImpl.java b/src/main/java/com/dji/sample/component/oss/service/impl/AmazonS3ServiceImpl.java
index 6d0b8af..fa896b6 100644
--- a/src/main/java/com/dji/sample/component/oss/service/impl/AmazonS3ServiceImpl.java
+++ b/src/main/java/com/dji/sample/component/oss/service/impl/AmazonS3ServiceImpl.java
@@ -5,9 +5,7 @@
 import com.amazonaws.auth.BasicAWSCredentials;
 import com.amazonaws.services.s3.AmazonS3;
 import com.amazonaws.services.s3.AmazonS3ClientBuilder;
-import com.amazonaws.services.s3.model.BucketCrossOriginConfiguration;
-import com.amazonaws.services.s3.model.CORSRule;
-import com.amazonaws.services.s3.model.S3Object;
+import com.amazonaws.services.s3.model.*;
 import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
 import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder;
 import com.amazonaws.services.securitytoken.model.AssumeRoleRequest;
@@ -18,28 +16,28 @@
 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 org.springframework.beans.factory.annotation.Autowired;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.PostConstruct;
-import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
+import java.util.Objects;
 
 /**
  * @author sean
  * @version 1.0
  * @date 2022/4/27
  */
+@Slf4j
 @Service
 public class AmazonS3ServiceImpl implements IOssService {
 
-    @Autowired
-    private OssConfiguration configuration;
-
+    private AmazonS3 client;
+    
     @Override
     public String getOssType() {
         return OssTypeEnum.AWS.getType();
@@ -49,61 +47,55 @@
     public CredentialsDTO getCredentials() {
         AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()
                 .withCredentials(new AWSStaticCredentialsProvider(
-                        new BasicAWSCredentials(configuration.getAccessKey(), configuration.getSecretKey())))
-                .withRegion(configuration.getRegion()).build();
+                        new BasicAWSCredentials(OssConfiguration.accessKey, OssConfiguration.secretKey)))
+                .withRegion(OssConfiguration.region).build();
 
         AssumeRoleRequest request = new AssumeRoleRequest()
-                .withRoleArn(configuration.getRoleArn())
-                .withRoleSessionName(configuration.getRoleSessionName())
-                .withDurationSeconds(Math.toIntExact(configuration.getExpire()));
+                .withRoleArn(OssConfiguration.roleArn)
+                .withRoleSessionName(OssConfiguration.roleSessionName)
+                .withDurationSeconds(Math.toIntExact(OssConfiguration.expire));
         AssumeRoleResult result = stsClient.assumeRole(request);
         Credentials credentials = result.getCredentials();
-        stsClient.shutdown();
         return new CredentialsDTO(credentials);
     }
 
     @Override
     public URL getObjectUrl(String bucket, String objectKey) {
-        AmazonS3 client = this.createClient();
-        URL url = client.generatePresignedUrl(bucket, objectKey,
-                new Date(System.currentTimeMillis() + configuration.getExpire() * 1000), HttpMethod.GET);
-        client.shutdown();
-        return url;
+        return client.generatePresignedUrl(bucket, objectKey,
+                new Date(System.currentTimeMillis() + OssConfiguration.expire * 1000), HttpMethod.GET);
     }
 
     @Override
     public Boolean deleteObject(String bucket, String objectKey) {
-        AmazonS3 client = this.createClient();
+        if (!client.doesObjectExist(bucket, objectKey)) {
+            return true;
+        }
         client.deleteObject(bucket, objectKey);
-        client.shutdown();
         return true;
     }
 
-    public byte[] getObject(String bucket, String objectKey) {
-        AmazonS3 client = this.createClient();
-        S3Object object = client.getObject(bucket, objectKey);
-        InputStream stream = object.getObjectContent().getDelegateStream();
-        try {
-            return stream.readAllBytes();
-        } catch (IOException e) {
-            e.printStackTrace();
-        } finally {
-            try {
-                stream.close();
-                client.shutdown();
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
-        }
-        return new byte[0];
+    public InputStream getObject(String bucket, String objectKey) {
+        return client.getObject(bucket, objectKey).getObjectContent().getDelegateStream();
     }
 
-    private AmazonS3 createClient() {
-        return AmazonS3ClientBuilder.standard()
+    @Override
+    public void putObject(String bucket, String objectKey, InputStream input) {
+        if (client.doesObjectExist(bucket, objectKey)) {
+            throw new RuntimeException("The filename already exists.");
+        }
+        PutObjectResult objectResult = client.putObject(new PutObjectRequest(bucket, objectKey, input, new ObjectMetadata()));
+        log.info("Upload File: {}", objectResult.toString());
+    }
+
+    public void createClient() {
+        if (Objects.nonNull(this.client)) {
+            return;
+        }
+        this.client = AmazonS3ClientBuilder.standard()
                 .withCredentials(
                         new AWSStaticCredentialsProvider(
-                                new BasicAWSCredentials(configuration.getAccessKey(), configuration.getSecretKey())))
-                .withRegion(configuration.getRegion())
+                                new BasicAWSCredentials(OssConfiguration.accessKey, OssConfiguration.secretKey)))
+                .withRegion(OssConfiguration.region)
                 .build();
     }
 
@@ -112,7 +104,7 @@
      */
     @PostConstruct
     private void configCORS() {
-        if (!configuration.isEnable() || !OssTypeEnum.AWS.getType().equals(configuration.getProvider())) {
+        if (!OssConfiguration.enable || !OssTypeEnum.AWS.getType().equals(OssConfiguration.provider)) {
             return;
         }
         List<CORSRule.AllowedMethods> allowedMethods = new ArrayList<>();
@@ -126,10 +118,8 @@
                 .withAllowedHeaders(List.of(AuthInterceptor.PARAM_TOKEN))
                 .withAllowedMethods(allowedMethods);
 
-        AmazonS3 client = this.createClient();
-
-        client.setBucketCrossOriginConfiguration(this.configuration.getBucket(),
+        client.setBucketCrossOriginConfiguration(OssConfiguration.bucket,
                 new BucketCrossOriginConfiguration().withRules(rule));
-        client.shutdown();
+        
     }
 }

--
Gitblit v1.9.3