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/AliyunOssServiceImpl.java |   75 +++++++++++++++++++++++++++----------
 1 files changed, 55 insertions(+), 20 deletions(-)

diff --git a/src/main/java/com/dji/sample/component/oss/service/impl/AliyunOssServiceImpl.java b/src/main/java/com/dji/sample/component/oss/service/impl/AliyunOssServiceImpl.java
index 5ffae6c..6717849 100644
--- a/src/main/java/com/dji/sample/component/oss/service/impl/AliyunOssServiceImpl.java
+++ b/src/main/java/com/dji/sample/component/oss/service/impl/AliyunOssServiceImpl.java
@@ -1,22 +1,28 @@
 package com.dji.sample.component.oss.service.impl;
 
 import com.aliyun.oss.OSS;
+import com.aliyun.oss.OSSClientBuilder;
+import com.aliyun.oss.OSSException;
+import com.aliyun.oss.model.ObjectMetadata;
+import com.aliyun.oss.model.PutObjectRequest;
+import com.aliyun.oss.model.PutObjectResult;
 import com.aliyuncs.DefaultAcsClient;
 import com.aliyuncs.IAcsClient;
 import com.aliyuncs.exceptions.ClientException;
 import com.aliyuncs.profile.DefaultProfile;
 import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
 import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
-import com.dji.sample.component.oss.model.AliyunOSSConfiguration;
+import com.dji.sample.component.oss.model.OssConfiguration;
+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 lombok.extern.slf4j.Slf4j;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
-import org.springframework.util.StringUtils;
 
+import java.io.InputStream;
 import java.net.URL;
 import java.util.Date;
+import java.util.Objects;
 
 /**
  * @author sean
@@ -27,24 +33,28 @@
 @Slf4j
 public class AliyunOssServiceImpl implements IOssService {
 
-    @Autowired(required = false)
     private OSS ossClient;
+    
+    @Override
+    public String getOssType() {
+        return OssTypeEnum.ALIYUN.getType();
+    }
 
     @Override
     public CredentialsDTO getCredentials() {
 
         try {
             DefaultProfile profile = DefaultProfile.getProfile(
-                    AliyunOSSConfiguration.region, AliyunOSSConfiguration.accessKey, AliyunOSSConfiguration.secretKey);
+                    OssConfiguration.region, OssConfiguration.accessKey, OssConfiguration.secretKey);
             IAcsClient client = new DefaultAcsClient(profile);
 
             AssumeRoleRequest request = new AssumeRoleRequest();
-            request.setDurationSeconds(AliyunOSSConfiguration.expire);
-            request.setRoleArn(AliyunOSSConfiguration.roleArn);
-            request.setRoleSessionName(AliyunOSSConfiguration.roleSessionName);
+            request.setDurationSeconds(OssConfiguration.expire);
+            request.setRoleArn(OssConfiguration.roleArn);
+            request.setRoleSessionName(OssConfiguration.roleSessionName);
 
             AssumeRoleResponse response = client.getAcsResponse(request);
-            return new CredentialsDTO(response.getCredentials(), AliyunOSSConfiguration.expire);
+            return new CredentialsDTO(response.getCredentials(), OssConfiguration.expire);
 
         } catch (ClientException e) {
             log.debug("Failed to obtain sts.");
@@ -55,19 +65,44 @@
 
     @Override
     public URL getObjectUrl(String bucket, String objectKey) {
-        if (!StringUtils.hasText(bucket) || !StringUtils.hasText(objectKey)) {
-            return null;
+        // First check if the object can be fetched.
+        boolean isExist = ossClient.doesObjectExist(bucket, objectKey);
+        if (!isExist) {
+            throw new OSSException("The object does not exist.");
         }
-        try {
-            // First check if the object can be fetched.
-            ossClient.getObject(bucket, objectKey);
 
-            return ossClient.generatePresignedUrl(bucket, objectKey,
-                    new Date(System.currentTimeMillis() + AliyunOSSConfiguration.expire * 1000));
-        } catch (NullPointerException e) {
-            e.printStackTrace();
-        }
-        return null;
+        return ossClient.generatePresignedUrl(bucket, objectKey,
+                new Date(System.currentTimeMillis() + OssConfiguration.expire * 1000));
     }
 
+    @Override
+    public Boolean deleteObject(String bucket, String objectKey) {
+        if (!ossClient.doesObjectExist(bucket, objectKey)) {
+            return true;
+        }
+        ossClient.deleteObject(bucket, objectKey);
+        return true;
+    }
+
+    @Override
+    public InputStream getObject(String bucket, String objectKey) {
+        return ossClient.getObject(bucket, objectKey).getObjectContent();
+    }
+
+    @Override
+    public void putObject(String bucket, String objectKey, InputStream input) {
+        if (ossClient.doesObjectExist(bucket, objectKey)) {
+            throw new RuntimeException("The filename already exists.");
+        }
+        PutObjectResult objectResult = ossClient.putObject(new PutObjectRequest(bucket, objectKey, input, new ObjectMetadata()));
+        log.info("Upload File: {}", objectResult.getETag());
+    }
+
+    public void createClient() {
+        if (Objects.nonNull(this.ossClient)) {
+            return;
+        }
+        this.ossClient = new OSSClientBuilder()
+                .build(OssConfiguration.endpoint, OssConfiguration.accessKey, OssConfiguration.secretKey);
+    }
 }

--
Gitblit v1.9.3