From 56df98ce4952239fbf7d0e99dbeb0e5c71531d6f Mon Sep 17 00:00:00 2001
From: sean.zhou <sean.zhou@dji.com>
Date: Fri, 18 Nov 2022 18:29:06 +0800
Subject: [PATCH] initial v1.3.0
---
src/main/java/com/dji/sample/component/oss/service/impl/MinIOServiceImpl.java | 46 +++++++++++++++++++++++++++++++++++-----------
1 files changed, 35 insertions(+), 11 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..b162751 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,10 +4,7 @@
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;
@@ -15,11 +12,13 @@
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
@@ -29,6 +28,8 @@
@Service
@Slf4j
public class MinIOServiceImpl implements IOssService {
+
+ private MinioClient client;
@Autowired
private OssConfiguration configuration;
@@ -87,21 +88,44 @@
}
@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 = this.createClient().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();
+ }
+
+ @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) {
+ 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();
+ }
+ }
}
private MinioClient createClient() {
- return MinioClient.builder()
+ if (Objects.nonNull(this.client)) {
+ return this.client;
+ }
+ this.client = MinioClient.builder()
.endpoint(configuration.getEndpoint())
.credentials(configuration.getAccessKey(), configuration.getSecretKey())
.region(configuration.getRegion())
.build();
+ return this.client;
}
}
--
Gitblit v1.9.3