使用SpringBoot集成Minio实现文件上传、下载、删除功能
创始人
2024-12-27 09:38:14
0

1. 添加依赖

首先,在你的Spring Boot项目中添加Minio的依赖。在 pom.xml 文件中添加以下依赖:

     io.minio     minio     你的版本  

2. 配置application.properties

application.properties 中配置Minio连接信息:

# Minio文件服务配置 minio:   # 访问服务地址   url: "http://127.0.0.1:5000"   # 用户名   user-name: "madmin"   # mi码   password: "madmin"   # 桶的名称   bucket-name: "test" 

3. 创建Minio配置类

创建一个配置类 MinioConfig.java,用于初始化Minio客户端:

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("${minio.endpoint}")     private String endpoint;      @Value("${minio.accessKey}")     private String accessKey;      @Value("${minio.secretKey}")     private String secretKey;      @Bean     public MinioClient minioClient() {         return MinioClient.builder()                 .endpoint(endpoint)                 .credentials(accessKey, secretKey)                 .build();     } } 

4. 实现文件上传、下载、删除操作

创建一个服务类 MinioFileService.java,实现文件上传、下载、删除等操作:

import io.minio.*; import io.minio.errors.MinioException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile;  import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.stream.Collectors;  @Service public class MinioFileService {      @Autowired     private MinioClient minioClient;      @Value("${minio.bucketName}")     private String bucketName;      // 上传文件     public String uploadFile(MultipartFile file) throws IOException {         String fileName = StringUtils.cleanPath(file.getOriginalFilename());         try (InputStream inputStream = file.getInputStream()) {             minioClient.putObject(                     PutObjectArgs.builder()                             .bucket(bucketName)                             .object(fileName)                             .stream(inputStream, inputStream.available(), -1)                             .contentType(file.getContentType())                             .build());         } catch (MinioException | IOException e) {             throw new IOException("File upload failed: " + e.getMessage());         }         return fileName;     }      // 下载文件     public InputStream downloadFile(String fileName) throws IOException {         try {             return minioClient.getObject(                     GetObjectArgs.builder()                             .bucket(bucketName)                             .object(fileName)                             .build());         } catch (MinioException e) {             throw new IOException("File download failed: " + e.getMessage());         }     }      // 删除文件     public void deleteFile(String fileName) throws IOException {         try {             minioClient.removeObject(                     RemoveObjectArgs.builder()                             .bucket(bucketName)                             .object(fileName)                             .build());         } catch (MinioException e) {             throw new IOException("File deletion failed: " + e.getMessage());         }     }      // 获取所有文件列表     public List getAllFiles() throws IOException {         try {             return minioClient.listObjects(                     ListObjectsArgs.builder()                             .bucket(bucketName)                             .build())                     .stream()                     .map(Result::get)                     .map(Item::objectName)                     .collect(Collectors.toList());         } catch (MinioException e) {             throw new IOException("Failed to retrieve file list: " + e.getMessage());         }     } } 

5. 创建Controller层接口

创建一个Controller类 FileController.java,定义REST接口,实现文件上传、下载、删除的调用:

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.util.List;  @RestController @RequestMapping("/files") public class FileController {      @Autowired     private MinioFileService minioFileService;      // 文件上传     @PostMapping("/upload")     public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {         return minioFileService.uploadFile(file);     }      // 文件下载     @GetMapping("/download/{fileName:.+}")     public void downloadFile(@PathVariable String fileName, HttpServletResponse response) throws IOException {         InputStream is = minioFileService.downloadFile(fileName);         response.setContentType("application/octet-stream");         response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");         org.apache.commons.io.IOUtils.copy(is, response.getOutputStream());         response.flushBuffer();     }      // 文件删除     @DeleteMapping("/delete/{fileName:.+}")     public void deleteFile(@PathVariable String fileName) throws IOException {         minioFileService.deleteFile(fileName);     }      // 获取所有文件列表     @GetMapping("/list")     public List getAllFiles() throws IOException {         return minioFileService.getAllFiles();     } } 

6. 测试接口

启动Spring Boot应用程序,使用Postman或浏览器访问接口测试文件上传、下载、删除功能。

这样,你就完成了Spring Boot与Minio的集成,实现了基本的文件上传、下载、删除操作。

相关内容

热门资讯

专业讨论!德扑之星真破解套路(... 专业讨论!德扑之星真破解套路(辅助挂)软件透明挂(有挂了解)-哔哩哔哩;人气非常高,ai更新快且高清...
每日必看!智星德州菠萝外挂检测... 每日必看!智星德州菠萝外挂检测(辅助挂)软件透明挂(有挂教学)-哔哩哔哩1、玩家可以在智星德州菠萝外...
透视透明挂!轰趴十三水有后台(... 轰趴十三水有后台赢率提升策略‌;透视透明挂!轰趴十三水有后台(辅助挂)软件透明挂(有挂详情)-哔哩哔...
发现玩家!德扑ai助手软件(辅... 发现玩家!德扑ai助手软件(辅助挂)透视辅助(有挂教学)-哔哩哔哩;玩家在德扑ai助手软件中需先进行...
一分钟了解!x-poker辅助... 一分钟了解!x-poker辅助软件(辅助挂)辅助透视(有挂攻略)-哔哩哔哩1、每一步都需要思考,不同...
一分钟揭秘!德州最新辅助器(辅... 一分钟揭秘!德州最新辅助器(辅助挂)透视辅助(有挂攻略)-哔哩哔哩;德州最新辅助器最新版本免费下载安...
玩家攻略推荐!德州辅助(辅助挂... 玩家攻略推荐!德州辅助(辅助挂)辅助透视(有挂了解)-哔哩哔哩是由北京得德州辅助黑科技有限公司精心研...
揭秘真相!pokernow德州... 《揭秘真相!pokernow德州(辅助挂)辅助透视(有挂介绍)-哔哩哔哩》 pokernow德州软件...
五分钟了解!德州之星辅助器(辅... 五分钟了解!德州之星辅助器(辅助挂)辅助透视(有挂透明)-哔哩哔哩1、很好的工具软件,可以解锁游戏的...
推荐一款!pokermaste... 1、推荐一款!pokermaster有外挂(辅助挂)透视辅助(有挂教学)-哔哩哔哩;详细教程。2、p...