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 | 78 ++++++++++++++++++--------------------
1 files changed, 37 insertions(+), 41 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 4767aec..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
@@ -2,7 +2,10 @@
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
-import com.aliyun.oss.model.OSSObject;
+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;
@@ -14,14 +17,12 @@
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.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
+import java.util.Objects;
/**
* @author sean
@@ -32,9 +33,8 @@
@Slf4j
public class AliyunOssServiceImpl implements IOssService {
- @Autowired
- public OssConfiguration configuration;
-
+ private OSS ossClient;
+
@Override
public String getOssType() {
return OssTypeEnum.ALIYUN.getType();
@@ -45,16 +45,16 @@
try {
DefaultProfile profile = DefaultProfile.getProfile(
- configuration.getRegion(), configuration.getAccessKey(), configuration.getSecretKey());
+ OssConfiguration.region, OssConfiguration.accessKey, OssConfiguration.secretKey);
IAcsClient client = new DefaultAcsClient(profile);
AssumeRoleRequest request = new AssumeRoleRequest();
- request.setDurationSeconds(configuration.getExpire());
- request.setRoleArn(configuration.getRoleArn());
- request.setRoleSessionName(configuration.getRoleSessionName());
+ request.setDurationSeconds(OssConfiguration.expire);
+ request.setRoleArn(OssConfiguration.roleArn);
+ request.setRoleSessionName(OssConfiguration.roleSessionName);
AssumeRoleResponse response = client.getAcsResponse(request);
- return new CredentialsDTO(response.getCredentials(), configuration.getExpire());
+ return new CredentialsDTO(response.getCredentials(), OssConfiguration.expire);
} catch (ClientException e) {
log.debug("Failed to obtain sts.");
@@ -65,48 +65,44 @@
@Override
public URL getObjectUrl(String bucket, String objectKey) {
- if (!StringUtils.hasText(bucket) || !StringUtils.hasText(objectKey)) {
- return null;
- }
- OSS ossClient = this.createClient();
// First check if the object can be fetched.
- ossClient.getObject(bucket, objectKey);
+ boolean isExist = ossClient.doesObjectExist(bucket, objectKey);
+ if (!isExist) {
+ throw new OSSException("The object does not exist.");
+ }
return ossClient.generatePresignedUrl(bucket, objectKey,
- new Date(System.currentTimeMillis() + configuration.getExpire() * 1000));
+ new Date(System.currentTimeMillis() + OssConfiguration.expire * 1000));
}
@Override
public Boolean deleteObject(String bucket, String objectKey) {
- OSS ossClient = this.createClient();
+ if (!ossClient.doesObjectExist(bucket, objectKey)) {
+ return true;
+ }
ossClient.deleteObject(bucket, objectKey);
- ossClient.shutdown();
return true;
}
@Override
- public byte[] getObject(String bucket, String objectKey) {
- OSS ossClient = this.createClient();
- OSSObject object = ossClient.getObject(bucket, objectKey);
- InputStream stream = object.getObjectContent();
-
- try {
- return stream.readAllBytes();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- stream.close();
- ossClient.shutdown();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return new byte[0];
+ public InputStream getObject(String bucket, String objectKey) {
+ return ossClient.getObject(bucket, objectKey).getObjectContent();
}
- private OSS createClient() {
- return new OSSClientBuilder()
- .build(configuration.getEndpoint(), configuration.getAccessKey(), configuration.getSecretKey());
+ @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