From 41335f6b5f0ab484259e6020568c8707162823e3 Mon Sep 17 00:00:00 2001 From: nixj Date: Mon, 20 May 2024 14:19:07 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90add=E3=80=91minio=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/chint/manage/config/MinioConfig.java | 26 +++++ .../convert}/DataJdbcConfig.java | 2 +- .../convert}/OrderPageQueryReadConverter.java | 2 +- .../OrderPageQueryWriteConverter.java | 5 +- .../manage/controller/FileController.java | 52 +++++++++ .../manage/controller/ManageController.java | 2 - .../chint/manage/service/MinioService.java | 109 ++++++++++++++++++ src/main/resources/application.yml | 6 + 8 files changed, 196 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/chint/manage/config/MinioConfig.java rename src/main/java/com/chint/manage/{util => config/convert}/DataJdbcConfig.java (92%) rename src/main/java/com/chint/manage/{util => config/convert}/OrderPageQueryReadConverter.java (94%) rename src/main/java/com/chint/manage/{util => config/convert}/OrderPageQueryWriteConverter.java (86%) create mode 100644 src/main/java/com/chint/manage/controller/FileController.java create mode 100644 src/main/java/com/chint/manage/service/MinioService.java diff --git a/src/main/java/com/chint/manage/config/MinioConfig.java b/src/main/java/com/chint/manage/config/MinioConfig.java new file mode 100644 index 00000000..9e6f59c5 --- /dev/null +++ b/src/main/java/com/chint/manage/config/MinioConfig.java @@ -0,0 +1,26 @@ +package com.chint.manage.config; + +import io.minio.MinioClient; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class MinioConfig { + + @Value("${chint.minio.endpoint}") + private String endpoint; + + @Value("${chint.minio.accessKey}") + private String accessKey; + + @Value("${chint.minio.secretKey}") + private String secretKey; + @Bean + public MinioClient minioClient() { + return MinioClient.builder() + .endpoint(endpoint) + .credentials(accessKey, secretKey) + .build(); + } +} \ No newline at end of file diff --git a/src/main/java/com/chint/manage/util/DataJdbcConfig.java b/src/main/java/com/chint/manage/config/convert/DataJdbcConfig.java similarity index 92% rename from src/main/java/com/chint/manage/util/DataJdbcConfig.java rename to src/main/java/com/chint/manage/config/convert/DataJdbcConfig.java index 3c45527c..58df796b 100644 --- a/src/main/java/com/chint/manage/util/DataJdbcConfig.java +++ b/src/main/java/com/chint/manage/config/convert/DataJdbcConfig.java @@ -1,4 +1,4 @@ -package com.chint.manage.util; +package com.chint.manage.config.convert; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/src/main/java/com/chint/manage/util/OrderPageQueryReadConverter.java b/src/main/java/com/chint/manage/config/convert/OrderPageQueryReadConverter.java similarity index 94% rename from src/main/java/com/chint/manage/util/OrderPageQueryReadConverter.java rename to src/main/java/com/chint/manage/config/convert/OrderPageQueryReadConverter.java index e7faeae9..f356e08c 100644 --- a/src/main/java/com/chint/manage/util/OrderPageQueryReadConverter.java +++ b/src/main/java/com/chint/manage/config/convert/OrderPageQueryReadConverter.java @@ -1,4 +1,4 @@ -package com.chint.manage.util; +package com.chint.manage.config.convert; import com.chint.manage.entity.query.OrderPageQuery; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/src/main/java/com/chint/manage/util/OrderPageQueryWriteConverter.java b/src/main/java/com/chint/manage/config/convert/OrderPageQueryWriteConverter.java similarity index 86% rename from src/main/java/com/chint/manage/util/OrderPageQueryWriteConverter.java rename to src/main/java/com/chint/manage/config/convert/OrderPageQueryWriteConverter.java index 8950c157..1c92ffd7 100644 --- a/src/main/java/com/chint/manage/util/OrderPageQueryWriteConverter.java +++ b/src/main/java/com/chint/manage/config/convert/OrderPageQueryWriteConverter.java @@ -1,14 +1,11 @@ -package com.chint.manage.util; +package com.chint.manage.config.convert; import com.chint.manage.entity.query.OrderPageQuery; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.core.convert.converter.Converter; -import org.springframework.data.convert.ReadingConverter; import org.springframework.data.convert.WritingConverter; -import java.io.IOException; - @WritingConverter public class OrderPageQueryWriteConverter implements Converter { private final ObjectMapper objectMapper = new ObjectMapper(); diff --git a/src/main/java/com/chint/manage/controller/FileController.java b/src/main/java/com/chint/manage/controller/FileController.java new file mode 100644 index 00000000..543cfefd --- /dev/null +++ b/src/main/java/com/chint/manage/controller/FileController.java @@ -0,0 +1,52 @@ +package com.chint.manage.controller; + +import com.chint.infrastructure.util.Result; +import com.chint.manage.service.MinioService; +import io.swagger.annotations.ApiOperation; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.InputStreamResource; +import org.springframework.core.io.Resource; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import java.io.InputStream; + +import static com.chint.dama.dc.basic.Result.SUCCESS; + +@Slf4j +@RestController +@RequestMapping("/file") +public class FileController { + + @Autowired + private MinioService minioService; + + @ApiOperation("上传文件") + @PostMapping("/upload") + public Result uploadFile(@RequestParam("file") MultipartFile file) { + String filePath = minioService.uploadFile(file); + return Result.Success(SUCCESS, filePath); + } + + @ApiOperation("下载文件") + @GetMapping("/download/{filePath}") + public ResponseEntity downloadFile(@PathVariable String filePath) { + InputStream inputStream = minioService.downloadFile(filePath); + Resource resource = new InputStreamResource(inputStream); + // 设置响应头 + return ResponseEntity.ok() + .header("Content-Disposition", "attachment; filename=" + filePath) + .contentType(MediaType.parseMediaType("application/octet-stream")) + .body(resource); + } + +// @ApiOperation("删除文件") +// @PostMapping("/delete/{filePath}") +// public Result deleteFile(@PathVariable String filePath) { +// minioService.deleteFile(filePath); +// return Result.Success(SUCCESS); +// } +} diff --git a/src/main/java/com/chint/manage/controller/ManageController.java b/src/main/java/com/chint/manage/controller/ManageController.java index 2e5b54dd..3529d34f 100644 --- a/src/main/java/com/chint/manage/controller/ManageController.java +++ b/src/main/java/com/chint/manage/controller/ManageController.java @@ -1,7 +1,5 @@ package com.chint.manage.controller; -import com.chint.domain.aggregates.user.User; -import com.chint.infrastructure.util.BaseContext; import com.chint.infrastructure.util.PageResult; import com.chint.infrastructure.util.Result; import com.chint.manage.entity.dto.ItineraryPageDto; diff --git a/src/main/java/com/chint/manage/service/MinioService.java b/src/main/java/com/chint/manage/service/MinioService.java new file mode 100644 index 00000000..65b0671b --- /dev/null +++ b/src/main/java/com/chint/manage/service/MinioService.java @@ -0,0 +1,109 @@ +package com.chint.manage.service; + +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.lang.UUID; +import com.chint.domain.exceptions.CommandException; +import io.minio.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Objects; + +@Service +public class MinioService { + + @Autowired + private MinioClient minioClient; + + @Value("${chint.minio.bucketName}") + private String bucketName; + + /** + * 上传文件 + * @param file 要保存的文件 + */ + public String uploadFile(MultipartFile file) { + + try{ + //检查MinIO是否存在此bucket + boolean isExist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); + if(!isExist){ + //不存在就创建 + minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); + } + //上传文件到MinIO + String fileName = UUID.randomUUID().toString(); + InputStream inputStream = file.getInputStream(); + ObjectWriteResponse objectWriteResponse = minioClient.putObject(PutObjectArgs.builder() + .object(fileName.concat(".").concat(FileUtil.extName(file.getOriginalFilename()))) + .contentType(file.getContentType()) + .bucket(bucketName).stream(inputStream, inputStream.available(), -1).build()); + return objectWriteResponse.object(); + }catch(Exception e) { + throw new CommandException(e.getMessage()); + } + } + + /** + * 删除文件 + * @param minioFilePath MinIO的文件路径 src/test.doc 带后缀名 + */ + public void deleteFile(String minioFilePath) { + try { + minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(minioFilePath).build()); + } catch (Exception e) { + throw new CommandException(e.getMessage()); + } + } + + /** + * 是否存在文件 + * @param filePath MinIO上的文件路径 src/test.doc 带后缀名 + * @return + * @throws Exception + */ + public boolean doesObjectExist(String filePath) { + //MinIO客户端 + Boolean existsFlag = true; + GetObjectResponse getObjectResponse = null; + try { + GetObjectArgs getObjectArgs = GetObjectArgs.builder().object(filePath).bucket(bucketName).build(); + getObjectResponse = minioClient.getObject(getObjectArgs); + if (Objects.isNull(getObjectResponse)) { + existsFlag = false; + } + } catch (Exception ex) { + existsFlag = false; + } finally { + try { + if (!Objects.isNull(getObjectResponse)) { + getObjectResponse.close(); + } + } catch (IOException e) { + e.printStackTrace(); + existsFlag = false; + } + } + return existsFlag; + } + + + /** + * 从MinIO获取文件的流 + * @param filePath MinIO上的文件路径 src/test.doc 带后缀名 + * @throws Exception + */ + public InputStream downloadFile(String filePath){ + //MinIO客户端 + try { + return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(filePath).build()); + } catch (Exception e) { + throw new CommandException(e.getMessage()); + } + } + +} \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 186068ee..43e98540 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -20,3 +20,9 @@ spring: port: ${chint.redis.port} password: ${chint.redis.password} database: ${chint.redis.database} +chint: + minio: + endpoint: http://10.10.103.101:9666 + accessKey: zOOGC4sgxG2RI5LD + secretKey: dy0Js8G52zVG8cPaL5AVxtdFJdcraWG5 + bucketName: chint-smart-canteen-test