# minio **Repository Path**: 896934834/minio ## Basic Information - **Project Name**: minio - **Description**: 本项目为springboot3集成minio,需要jdk的版本为17 主要功能有:文件上传、预览文件、下载文件、删除文件 关键词: springboot3, minio, springboot+minio - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-02-07 - **Last Updated**: 2025-04-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # minio ### 一、项目简介 本项目为springboot3集成minio,需要jdk的版本为17 主要功能有:文件上传、预览文件、下载文件、删除文件 * [windows安装openjdk17](https://gitee.com/896934834/environment-setup/blob/master/windows/jdk/1.windows%E5%AE%89%E8%A3%85openjdk17.md) #### 二、技术栈 * 1.springboot3 * 2.minio ### 三、windows安装与启动MinIO * [windows安装与启动MinIO](https://gitee.com/896934834/blog/blob/master/minio/windows%E5%AE%89%E8%A3%85%E4%B8%8E%E5%90%AF%E5%8A%A8MinIO.md) ### 四、集成步骤 * 1.启动MinIO服务,创建minio-boot的Bucket ``` d: cd work\minio\bin .\minio.exe server D:\work\minio\data --console-address "127.0.0.1:9000" --address "127.0.0.1:9005" ``` minio启动 ![输入图片说明](doc/readme/images/1.png) minio登录 ![输入图片说明](doc/readme/images/2.png) 创建名称为minio-boot的Bucket ![输入图片说明](doc/readme/images/3.png) * 2.pom.xml文件中添加MinIO的依赖 ``` io.minio minio 8.5.17 ``` * 3.在application.yml文件中配置MinIO的相关参数 ``` server: port: 8080 minio: #endpoint: "http://127.0.0.1:9000" endpoint: "http://127.0.0.1:9005" access-key: "admin" secret-key: "tom250206" bucket-name: "minio-boot" ``` * 4.创建一个MinioConfig类,用于加载MinIO的配置信息 ``` package com.bigzero.minio.common.config; import io.minio.MinioClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * minio配置 * * @author fanxinxiong * @since 2025-02-07 */ @Configuration public class MinioConfig { @Value("${minio.endpoint}") private String minioEndpoint; @Value("${minio.access-key}") private String minioAccessKey; @Value("${minio.secret-key}") private String minioSecretKey; @Bean public MinioClient minioClient(){ return MinioClient.builder() .endpoint(minioEndpoint) .credentials(minioAccessKey, minioSecretKey) .build(); } } ``` * 5.编写MinioUtil工具类 ``` package com.bigzero.minio.common.utils; import io.minio.*; import io.minio.http.Method; import jakarta.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; import java.util.concurrent.TimeUnit; /** * minio工具类 * * @author fanxinxiong * @since 2025-02-07 */ @Component public class MinioUtil { @Autowired private MinioClient minioClient; /** * 判断bucket是否存在,不存在则创建 */ public boolean existBucket(String bucketName){ boolean exists; try{ exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build()); if(!exists){ minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build()); exists = true; } }catch (Exception e){ e.printStackTrace(); exists = false; } return exists; } /** * 删除bucket */ public Boolean removeBucket(String bucketName){ try { minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build()); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * 上传文件 * @param file 文件 * @param fileName 文件名称 * @param bucketName bucket名称 */ public void upload(MultipartFile file, String fileName, String bucketName){ //使用putObject上传一个文件到存储桶中。 try { InputStream inputStream = file.getInputStream(); minioClient.putObject(PutObjectArgs.builder() .bucket(bucketName) .object(fileName) .stream(inputStream, file.getSize(), -1) .contentType(file.getContentType()) .build()); } catch (Exception e) { e.printStackTrace(); } } /** * 获取文件访问地址(有过期时间) * * @param fileName 文件名称 * @param time 时间 * @param timeUnit 时间单位 * @param bucketName bucket名称 */ public String getExpireFileUrl(String fileName, int time, TimeUnit timeUnit, String bucketName) { try { return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() .method(Method.GET) .bucket(bucketName) .object(fileName) .expiry(time, timeUnit).build()); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 获取文件访问地址 * * @param fileName 文件名称 * @param bucketName bucket名称 */ public String getFileUrl(String fileName, String bucketName){ try { return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder() .method(Method.GET) .bucket(bucketName) .object(fileName) .build() ); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 下载文件 * @param fileName 文件名称 * @param bucketName bucket名称 */ public void download(HttpServletResponse response, String fileName, String bucketName) { InputStream in = null; try { // 获取对象信息 StatObjectResponse stat = minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(fileName).build()); response.setContentType(stat.contentType()); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); // 文件下载 in = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build()); IOUtils.copy(in, response.getOutputStream()); } catch (Exception e) { e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 删除文件 * @param fileName 文件名称 * @param bucketName bucket名称 */ public void delete(String fileName, String bucketName) { try { minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(fileName).build()); } catch (Exception e) { e.printStackTrace(); } } } ``` * 6.编写controller类 ``` package com.bigzero.minio.common.controller; import com.bigzero.minio.common.utils.MinioUtil; import jakarta.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.util.UUID; /** * minio文件控制类 * * @author fanxinxiong * @since 2025-02-07 */ @RestController @RequestMapping("/common/minioFile") public class MinioFileController { @Value("${minio.bucket-name}") private String bucketName; @Autowired private MinioUtil minioUtil; /** * 上传文件 */ @PostMapping(value = "/upload") public String uploadReport(MultipartFile[] files) { for (MultipartFile file : files) { String fileName = file.getOriginalFilename(); String rename = UUID.randomUUID().toString(); minioUtil.upload(file, rename, bucketName); } return "上传成功"; } /** * 预览文件 */ @GetMapping("/preview") public String preview(String fileName) { return minioUtil.getFileUrl(fileName, bucketName); } /** * 下载文件 */ @GetMapping("/download") public void download(String fileName, HttpServletResponse response) { minioUtil.download(response, fileName, bucketName); } /** * 删除文件 */ @GetMapping("/delete") public String delete(String fileName){ minioUtil.delete(fileName, bucketName); return "删除成功"; } } ``` ### 五、启动项目验证 * 1.上传文件 ``` http://127.0.0.1:8080/common/minioFile/upload ``` ![输入图片说明](doc/readme/images/4.png) minio文件查看 ![输入图片说明](doc/readme/images/5.png) * 2.预览文件 ``` http://127.0.0.1:8080/common/minioFile/preview?fileName= ``` ![输入图片说明](doc/readme/images/6.png) 浏览器预览 ![输入图片说明](doc/readme/images/7.png) * 3.下载文件 ``` http://127.0.0.1:8080/common/minioFile/download?fileName= ``` ![输入图片说明](doc/readme/images/8.png) * 4.删除文件 ``` http://127.0.0.1:8080/common/minioFile/delete?fileName= ``` ![输入图片说明](doc/readme/images/9.png)