MinIO 是一种开源的对象存储服务,它基于云原生架构构建,并提供了高性能、易于扩展和安全的存储解决方案。
一.安装和配置 MinIO 服务器
为了演示方便,本文采用Windows安装
1.在官方网站下载MinIO 安装文件,地址:https://dl.minio.org.cn/server/minio/release/windows-amd64/minio.exe;
2.在minio.exe目录下创建start.bat配置启动文件:"D:\Java\MinIo"是服务启动时文件存放的位置
minio.exe server D:\Java\MinIo
3.双击minio.exe文件启动


4.创建一个 Spring Boot 项目
1.创建一个 Spring Boot 项目
2.添加依赖,注意依赖版本
- <dependency>
- <groupId>commons-iogroupId>
- <artifactId>commons-ioartifactId>
- <version>2.4version>
- dependency>
- <dependency>
- <groupId>commons-fileuploadgroupId>
- <artifactId>commons-fileuploadartifactId>
- <version>1.4version>
- dependency>
-
- <dependency>
- <groupId>io.miniogroupId>
- <artifactId>minioartifactId>
- <version>8.4.3version>
- dependency>
- <dependency>
- <groupId>com.squareup.okhttp3groupId>
- <artifactId>okhttpartifactId>
- <version>4.8.1version>
- dependency>
二.配置文件
1.yaml配置文件
- #MinIO配置
- minio:
- endpoint: http://127.0.0.01:9000 #连接地址
- accessKey: minioadmin#账号 默认minioadmin
- secretKey: minioadmin#密码 默认minioadmin
- bucketName: contractfile #桶名 存放合同文件 桶名校验规则:!name.matches("^[a-z0-9][a-z0-9\\.\\-]+[a-z0-9]$")
2.配置类,用来连接Minio
- @Data
- @Configuration
- @ConfigurationProperties(prefix = "minio")
- public class MinioConfig {
-
- //连接地址
- private String endpoint;
-
- //账号 默认minioadmin
- private String accessKey;
-
- //密码 默认minioadmin
- private String secretKey;
-
- @Bean
- public MinioClient minioClient() {
- MinioClient minioClient = MinioClient.builder()
- .endpoint(endpoint)
- .credentials(accessKey, secretKey)
- .build();
- return minioClient;
- }
-
- }
3.工具类,用来操作文件
- @Slf4j
- @Component
- public class MinioUtils {
-
- @Autowired
- private MinioClient minioClient;
-
- @Value("${minio.bucketName}")
- private String bucketName;
-
- /**
- * 操作文件时先创建Bucket
- * 如果没有Bucket则创建
- *
- * @param bucketName
- */
- @SneakyThrows(Exception.class)
- public void createBucket(String bucketName) {
- if (!bucketExists(bucketName)) {
- minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
- }
- }
-
- /**
- * 判断Bucket是否存在,true:存在,false:不存在
- *
- * @param bucketName
- * @return
- */
- @SneakyThrows(Exception.class)
- public boolean bucketExists(String bucketName) {
- return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
- }
-
-
- /**
- * 判断文件是否存在
- *
- * @param bucketName
- * @param fileRealName
- * @return
- */
- public boolean isObjectExist(String bucketName, String fileRealName) {
- boolean exist = true;
- try {
- minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(fileRealName).build());
- } catch (Exception e) {
- log.error("[Minio工具类]>>>> 判断文件是否存在, 异常:", e);
- exist = false;
- }
- return exist;
- }
-
- /**
- * 使用MultipartFile进行文件上传
- *
- * @param bucketName 存储桶
- * @param file 文件
- * @param fileRealName 文件名
- * @return 文件下载外链
- */
- @SneakyThrows(Exception.class)
- public String uploadFile(String bucketName, MultipartFile file, String fileRealName) {
- createBucket(bucketName);
- InputStream inputStream = file.getInputStream();
- minioClient.putObject(
- PutObjectArgs.builder()
- .bucket(bucketName)
- .object(fileRealName)
- .contentType(file.getContentType())
- .stream(inputStream, inputStream.available(), -1)
- .build());
- GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder()
- .bucket(bucketName)
- .object(fileRealName)
- .method(Method.GET).build();
- return minioClient.getPresignedObjectUrl(args);
- }
-
-
- /**
- * 删除文件
- *
- * @param bucketName 存储桶
- * @param fileRealName 文件名称
- */
- @SneakyThrows(Exception.class)
- public void removeFile(String bucketName, String fileRealName) {
- createBucket(bucketName);
- minioClient.removeObject(
- RemoveObjectArgs.builder()
- .bucket(bucketName)
- .object(fileRealName)
- .build());
- }
-
- /**
- * 下载文件
- *
- * @param httpServletResponse httpServletResponse
- * @param fileRealName 文件存储名称
- * @param fileName 文件下载名称
- * @throws IOException IOException
- */
- public void downloadFile(String bucketName, String fileRealName, String fileName, HttpServletResponse httpServletResponse) throws Exception {
- createBucket(bucketName);
- //获取文件流
- InputStream inputStream = minioClient.getObject(GetObjectArgs.builder()
- .bucket(bucketName)
- .object(fileRealName)
- .build());
- //设置响应头信息,告诉前端浏览器下载文件
- httpServletResponse.setContentType("application/octet-stream;charset=UTF-8");
- httpServletResponse.setCharacterEncoding("UTF-8");
- httpServletResponse.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
- //获取输出流进行写入数据
- OutputStream outputStream = httpServletResponse.getOutputStream();
- // 将输入流复制到输出流
- byte[] buffer = new byte[4096];
- int bytesRead = -1;
- while ((bytesRead = inputStream.read(buffer)) != -1) {
- outputStream.write(buffer, 0, bytesRead);
- }
- // 关闭流资源
- inputStream.close();
- outputStream.close();
- }
- }