springboot整合minio
1.添加依赖
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>7.1.0</version> </dependency>
2.添加配置
minio: endpoint: https://play.min.io # minio URL accessKey: Q3AM3UQ867SPQQA43P2F #账号 secretKey: zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG #密码
3.添加属性类
package com.example.mall2.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; /** * Author: zhangbicheng * Date: 2022/2/18 */ @Configuration @ConfigurationProperties(prefix = "minio") @Data public class MinioProperties { private String endpoint; private String accessKey; private String secretKey; }
4.添加配置类
package com.example.mall2.config; import io.minio.MinioClient; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * Author: zhangbicheng * Date: 2022/2/18 */ @Configuration public class MinioConfig { @Autowired private MinioProperties minioProperties; @Bean public MinioClient minioClient() { return MinioClient.builder() .endpoint(minioProperties.getEndpoint()) .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey()) .build(); } }
5.上传文件代码
package com.example.mall2.controller; import com.example.mall2.config.MinioConfig; import io.minio.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; /** * Author: zhangbicheng * Date: 2022/2/18 */ @RestController public class UploadController { @Autowired private MinioClient minioClient; @RequestMapping("/upload") public String upload(MultipartFile file) { try { minioClient.putObject( PutObjectArgs.builder() .bucket("gstszbc")//bucket名 .object(file.getOriginalFilename())//上传到minio文件名 .contentType(file.getContentType())//文件类型 .stream(file.getInputStream(), file.getSize(), -1) //文件流 .build()); System.out.println("my-filename is uploaded to my-objectname successfully"); return "11"; } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } } }
6.测试上传
7.下载文件
@RequestMapping("/download") public String download(HttpServletResponse response) { try { Iterable<Result<Item>> results = minioClient.listObjects( ListObjectsArgs.builder().bucket("gstszbc").build()//获取bucket里所有文件信息 ); String fileName = null; for (Result<Item> result : results) { Item item = result.get(); fileName = item.objectName(); System.out.println(item.lastModified() + "\t" + item.size() + "\t" + item.objectName()); } InputStream object = minioClient.getObject(GetObjectArgs.builder().bucket("gstszbc").object(fileName).build()); BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream()); IOUtils.copy(object, bos); response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8")); bos.flush(); return "success"; } catch (Exception e) { e.printStackTrace(); return e.getMessage(); } }
8.测试下载
说明:使用的是官网提供的服务,https://play.min.io,也可使用自己搭建的minio文件服务。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律