From 7defb78dfa9ee3270c3c049d017918724f7e4b1c Mon Sep 17 00:00:00 2001 From: weizhihong Date: Fri, 30 Sep 2022 15:33:55 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90minio=E9=A2=84=E5=A4=84=E7=90=86?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E6=8E=A5=E5=8F=A3=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rtss/controller/MinioController.java | 6 +++++ .../joylink/rtss/services/MinioService.java | 3 +++ .../rtss/services/MinioServiceImpl.java | 7 ++++++ .../joylink/rtss/util/MinioClientUtil.java | 23 ++++++++++++++++++- 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/main/java/club/joylink/rtss/controller/MinioController.java b/src/main/java/club/joylink/rtss/controller/MinioController.java index a786ba395..70535d182 100644 --- a/src/main/java/club/joylink/rtss/controller/MinioController.java +++ b/src/main/java/club/joylink/rtss/controller/MinioController.java @@ -5,6 +5,7 @@ import club.joylink.rtss.services.MinioService; import club.joylink.rtss.vo.AccountVO; import club.joylink.rtss.vo.client.PageVO; import club.joylink.rtss.vo.minio.MinioFilePageVO; +import io.minio.http.Method; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -17,6 +18,11 @@ public class MinioController { @Autowired private MinioService minioService; + @GetMapping("/perSignedUrl") + public String getPerSignedObjectUrl(String directory, String fileName, Method method) { + return minioService.preSignedObjectUrl(directory, fileName, method); + } + @PostMapping("/file/save") public void saveFile(@RequestAttribute AccountVO user, @RequestBody MinioFileInfo fileInfo) { minioService.saveFileInfo(user, fileInfo); diff --git a/src/main/java/club/joylink/rtss/services/MinioService.java b/src/main/java/club/joylink/rtss/services/MinioService.java index 8feeb00dd..4d50201d4 100644 --- a/src/main/java/club/joylink/rtss/services/MinioService.java +++ b/src/main/java/club/joylink/rtss/services/MinioService.java @@ -5,6 +5,7 @@ import club.joylink.rtss.util.MinioClientUtil; import club.joylink.rtss.vo.AccountVO; import club.joylink.rtss.vo.client.PageVO; import club.joylink.rtss.vo.minio.MinioFilePageVO; +import io.minio.http.Method; public interface MinioService { /** @@ -12,6 +13,8 @@ public interface MinioService { */ MinioClientUtil createMinioClientUtil(String directory); + String preSignedObjectUrl(String directory, String fileName, Method method); + /** * 前端保存minio上传的文件信息 */ diff --git a/src/main/java/club/joylink/rtss/services/MinioServiceImpl.java b/src/main/java/club/joylink/rtss/services/MinioServiceImpl.java index edc34c009..20066b45c 100644 --- a/src/main/java/club/joylink/rtss/services/MinioServiceImpl.java +++ b/src/main/java/club/joylink/rtss/services/MinioServiceImpl.java @@ -10,6 +10,7 @@ import club.joylink.rtss.vo.client.PageVO; import club.joylink.rtss.vo.minio.MinioFilePageVO; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; +import io.minio.http.Method; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @@ -38,6 +39,12 @@ public class MinioServiceImpl implements MinioService { return minioClientUtil; } + @Override + public String preSignedObjectUrl(String directory, String fileName, Method method) { + MinioClientUtil minioClientUtil = createMinioClientUtil(directory); + return minioClientUtil.getPreSignedObjectUrl(fileName, method); + } + @Override public void saveFileInfo(AccountVO user, MinioFileInfo minioFileInfo) { minioFileInfo.setCreator(user.getId()); diff --git a/src/main/java/club/joylink/rtss/util/MinioClientUtil.java b/src/main/java/club/joylink/rtss/util/MinioClientUtil.java index 19d83afb8..b85bb0890 100644 --- a/src/main/java/club/joylink/rtss/util/MinioClientUtil.java +++ b/src/main/java/club/joylink/rtss/util/MinioClientUtil.java @@ -18,12 +18,14 @@ import java.io.*; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.Base64; +import java.util.HashMap; +import java.util.Map; import java.util.stream.Collectors; public class MinioClientUtil { private final static String PATH_SEPARATOR = "/"; - + private MinioClientConfig config; /** @@ -221,6 +223,25 @@ public class MinioClientUtil { } } + /** + * 获取预处理连接 + */ + public String getPreSignedObjectUrl(String fileName, Method method) { + try { + Map reqParams = new HashMap(); + reqParams.put("response-content-type", "application/json"); + return config.getMinioClient().getPresignedObjectUrl( + GetPresignedObjectUrlArgs.builder().method(method) + .bucket(config.getBucket()).object(getSavePath(fileName)) + .expiry(60 * 60 * 24) + .extraQueryParams(reqParams) + .build() + ); + } catch (Exception e) { + throw new SimulationException(SimulationExceptionType.System_Fault, e); + } + } + private String getSavePath(String fileName) { return FileInfo.getStoragePath(directory, fileName); }