From 36eaabeac65c45621ed2054ff55289164c2f5843 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 15 Jun 2023 14:29:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8F=90=E4=BE=9BMinIO=E6=B5=81?= =?UTF-8?q?=E5=BC=8F=E5=A4=84=E7=90=86=E6=96=B9=E5=BC=8F=E9=80=90=E5=9D=97?= =?UTF-8?q?=E5=9C=B0=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/sdk/file/FileRepository.java | 20 ++++++++++++- .../metersphere/sdk/file/MinioRepository.java | 30 +++++++++++++++++-- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/FileRepository.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/FileRepository.java index eef248948e..40bdcd8e57 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/FileRepository.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/FileRepository.java @@ -32,7 +32,16 @@ public interface FileRepository { public void delete(FileRequest request) throws Exception; /** - * 获取文件字节内容 + * 删除文件夹 + * + * @param request + * @throws Exception + */ + public void deleteFolder(FileRequest request) throws Exception; + + + /** + * 获取文件字节内容,大文件不建议使用 * * @param request * @return @@ -40,4 +49,13 @@ public interface FileRepository { */ public byte[] getFile(FileRequest request) throws Exception; + /** + * 流式处理方式,通过逐块地下载文件 + * + * @param request + * @param localPath + * @throws Exception + */ + public void downloadFile(FileRequest request, String localPath) throws Exception; + } diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/MinioRepository.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/MinioRepository.java index df9675d816..4568ce9667 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/MinioRepository.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/file/MinioRepository.java @@ -8,9 +8,7 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.InputStream; +import java.io.*; import java.util.ArrayList; import java.util.List; @@ -19,6 +17,8 @@ public class MinioRepository implements FileRepository { @Resource private MinioClient client; + // 缓冲区大小 + private static final int BUFFER_SIZE = 8192; private String getPath(FileRequest request) { // 文件存储路径 @@ -61,6 +61,12 @@ public class MinioRepository implements FileRepository { removeObject(MinioConfig.BUCKET, filePath); } + @Override + public void deleteFolder(FileRequest request) throws Exception { + String filePath = getPath(request); + // 删除文件夹 + removeObjects(MinioConfig.BUCKET, filePath); + } private boolean removeObject(String bucketName, String objectName) throws Exception { client.removeObject(RemoveObjectArgs.builder() @@ -104,6 +110,24 @@ public class MinioRepository implements FileRepository { return getFileAsStream(request).readAllBytes(); } + @Override + public void downloadFile(FileRequest request, String fullPath) throws Exception { + String fileName = getPath(request); + // 下载对象到本地文件 + try (InputStream inputStream = client.getObject( + GetObjectArgs.builder() + .bucket(MinioConfig.BUCKET) + .object(fileName) + .build()); + BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(fullPath))) { + byte[] buffer = new byte[BUFFER_SIZE]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + } + } + public InputStream getFileAsStream(FileRequest request) throws Exception { String fileName = getPath(request); return client.getObject(GetObjectArgs.builder()