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/MinIOServiceImpl.java | 74 ++++++++++++++++++++++---------------
1 files changed, 44 insertions(+), 30 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 009a1fb..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
@@ -4,22 +4,20 @@
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.GetObjectArgs;
-import io.minio.GetPresignedObjectUrlArgs;
-import io.minio.MinioClient;
-import io.minio.RemoveObjectArgs;
+import io.minio.*;
import io.minio.credentials.AssumeRoleProvider;
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;
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
@@ -30,9 +28,8 @@
@Slf4j
public class MinIOServiceImpl implements IOssService {
- @Autowired
- private OssConfiguration configuration;
-
+ private MinioClient client;
+
@Override
public String getOssType() {
return OssTypeEnum.MINIO.getType();
@@ -41,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();
@@ -56,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) {
@@ -87,21 +80,42 @@
}
@Override
- public byte[] getObject(String bucket, String objectKey) {
- MinioClient client = this.createClient();
- try (InputStream objectResponse = client.getObject(GetObjectArgs.builder().bucket(bucket).object(objectKey).build())) {
- return objectResponse.readAllBytes();
- } catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException e) {
+ public InputStream getObject(String bucket, String objectKey) {
+ try {
+ 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();
}
- return new byte[0];
+ return InputStream.nullInputStream();
}
- private MinioClient createClient() {
- return MinioClient.builder()
- .endpoint(configuration.getEndpoint())
- .credentials(configuration.getAccessKey(), configuration.getSecretKey())
- .region(configuration.getRegion())
+ @Override
+ public void putObject(String bucket, String objectKey, InputStream input) {
+ try {
+ 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();
+ }
+ }
+ }
+
+ public void createClient() {
+ if (Objects.nonNull(this.client)) {
+ return;
+ }
+ this.client = MinioClient.builder()
+ .endpoint(OssConfiguration.endpoint)
+ .credentials(OssConfiguration.accessKey, OssConfiguration.secretKey)
+ .region(OssConfiguration.region)
.build();
}
}
--
Gitblit v1.9.3