1. 미니IO
MinIO
Apache License v2.0 오픈 소스 계약을 기반으로 하는 객체 스토리지 서비스 입니다 . Amazon S3 클라우드 스토리지 서비스 인터페이스와 호환되어 사진, 동영상, 로그 파일, 백업 데이터, 컨테이너/가상 머신 이미지 등 대용량 비정형 데이터를 저장하는 데 매우 적합하며, 객체 파일을 저장할 수 있습니다. kb에서 최대 5T까지 여러 범위에 이르는 모든 크기.
MinIO
NodeJS, Redis 또는 MySQL과 같은 다른 애플리케이션과 쉽게 통합할 수 있는 매우 가벼운 서비스입니다.
2. MinIO 설치 및 시작
MinIO는 별도의 서버이므로 별도로 배포해야 하므로 Windows 시스템에서 MinIO를 사용하는 방법은 다음 블로그를 확인하시기 바랍니다.
https://blog.csdn.net/Angel_asp/article/details/128544612
기본 계정과 비밀번호는 minioadmin입니다.
3. pom.xml
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<!-- SpringBoot Web容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.3.4</version>
</dependency>
4. applicatin.properties(구성 파일)
# 设置单个文件大小
spring.servlet.multipart.max-file-size= 50MB
#minio文件服务器配置
s3.url=http://localhost:9000
s3.accessKey=admin
s3.secretKey=admin123
s3.bucketName=test
5. Java 비즈니스 클래스 작성
minio와 관련된 방법에는 버킷 존재 여부 확인, 버킷 생성, 파일 업로드, 파일 읽기, 파일 다운로드, 파일 삭제 등이 포함됩니다.
1. StorageProperty 스토리지 속성 클래스:
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Author yang
* @Date 2023/1/3 14:00
* @Version 1.0
*/
@Data
@Component
@ConfigurationProperties(prefix = "s3")
public class StorageProperty {
private String url;
private String accessKey;
private String secretKey;
// private long callTimeOut = 60000;
// private long readTimeOut = 300000;
}
2. 미니오 구성 클래스:
import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import io.minio.messages.Bucket;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.List;
/**
* @Author yang
* @Date 2023/1/3 14:03
* @Version 1.0
*/
@Slf4j
@Component
@Configuration
public class MinioClientConfig {
@Autowired
private StorageProperty storageProperty;
private static MinioClient minioClient;
/**
* @description: 获取minioClient
* @date 2021/6/22 16:55
* @return io.minio.MinioClient
*/
public static MinioClient getMinioClient(){
return minioClient;
}
/**
* 判断 bucket是否存在
*
* @param bucketName:
* 桶名
* @return: boolean
* @date : 2020/8/16 20:53
*/
@SneakyThrows(Exception.class)
public static boolean bucketExists(String bucketName) {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
}
/**
* 获取全部bucket
*
* @param :
* @return: java.util.List<io.minio.messages.Bucket>
* @date : 2020/8/16 23:28
*/
@SneakyThrows(Exception.class)
public static List<Bucket> getAllBuckets() {
return minioClient.listBuckets();
}
/**
* 初始化minio配置
*
* @param :
* @return: void
* @date : 2020/8/16 20:56
*/
@PostConstruct
public void init() {
try {
minioClient = MinioClient.builder()
.endpoint(storageProperty.getUrl())
.credentials(storageProperty.getAccessKey(), storageProperty.getSecretKey())
.build();
} catch (Exception e) {
e.printStackTrace();
log.error("初始化minio配置异常: 【{}】", e.fillInStackTrace());
}
}
}
六、 MinIoController
파일 업로드, 파일 읽기, 파일 다운로드, 파일 삭제를 위한 인터페이스는 다음과 같습니다.
/**
* @Author yangb
* @Date 2022/11/27 15:55
* @Version 1.0
*/
@RestController
@RequestMapping("/minio")
public class MinIoController extends BaseController {
MinioUtil minioUtil = new MinioUtil();
/**
* 上传文件
* @param file
* @return
*/
@PostMapping("/uploadFile")
public AjaxResult uploadFile(@RequestBody MultipartFile file) {
MinioClient minioClient = MinioClientConfig.getMinioClient();
if (minioClient == null) {
return AjaxResult.error("连接MinIO服务器失败", null);
}
ResultEntity<Map<String, Object>> result = minioUtil.minioUpload(file, "", "data-enpower");
if (result.getCode() == 0) {
return AjaxResult.success("上传成功");
} else {
return AjaxResult.error("上传错误!!!");
}
}
/**
* 获取文件预览地址
* @param fileName
* @return
*/
@RequestMapping("/getRedFile")
public AjaxResult getRedFile(@RequestBody String fileName) {
MinioClient minioClient = MinioClientConfig.getMinioClient();
if (minioClient == null) {
return AjaxResult.error("连接MinIO服务器失败", null);
}
String url = minioUtil.getPreviewFileUrl("data-enpower",fileName);
return AjaxResult.success(url);
}
/**
* 下载文件
* @param fileName
* @param response
* @return
*/
@RequestMapping("/downloadFile")
public String downloadFile(@RequestParam String fileName, HttpServletResponse response) {
MinioClient minioClient = MinioClientConfig.getMinioClient();
if (minioClient == null) {
return "连接MinIO服务器失败";
}
return minioUtil.downloadFile("data-enpower",fileName,response) != null ? "下载成功" : "下载失败";
}
/**
* 删除文件
*
* @param fileName 文件路径
* @return
*/
@PostMapping("/deleteFile")
public String deleteFile(String fileName) {
MinioClient minioClient = MinioClientConfig.getMinioClient();
if (minioClient == null) {
return "连接MinIO服务器失败";
}
boolean flag = minioUtil.deleteFile("data-enpower",fileName);
return flag == true ? "删除成功" : "删除失败";
}
}
7. 디버깅 결과
1. 파일 업로드
미니오의 파일
2. 파일 다운로드
3. 파일 삭제
minio에서 파일이 삭제되었는지 확인해보자
출처: blog.csdn.net/Angel_asp/article/* 세부정보/128535684