【add】minio文件管理功能实现
This commit is contained in:
parent
31ca86fa2e
commit
41335f6b5f
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
|
@ -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;
|
|
@ -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<OrderPageQuery, String> {
|
||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
@ -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<Object> uploadFile(@RequestParam("file") MultipartFile file) {
|
||||
String filePath = minioService.uploadFile(file);
|
||||
return Result.Success(SUCCESS, filePath);
|
||||
}
|
||||
|
||||
@ApiOperation("下载文件")
|
||||
@GetMapping("/download/{filePath}")
|
||||
public ResponseEntity<Resource> 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<String> deleteFile(@PathVariable String filePath) {
|
||||
// minioService.deleteFile(filePath);
|
||||
// return Result.Success(SUCCESS);
|
||||
// }
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue