简介
Minio支持接入JavaScript、Java、Python、Golang等多种语言,这里我们选择最熟悉的Java语言,使用最流行的框架 SpringBoot 2.x。
Spring boot 集成 Minio
这里使用 Spring Boot Starter Minio集成。
添加pom依赖
1 2 3 4 5
| <dependency> <groupId>com.jlefebure</groupId> <artifactId>spring-boot-starter-minio</artifactId> <version>1.1</version> </dependency>
|
修改配置文件
1 2 3 4 5
| minio: url: http://192.168.0.247:9000 bucket: test access-key: minioadmin secret-key: minioadmin
|
测试接口类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| @RestController @RequestMapping("/files") public class MinioController {
@Autowired private MinioService minioService;
@GetMapping("/list") public List<Item> testMinio() throws MinioException { return minioService.list(); }
@GetMapping("/{object}") public void getObject(@PathVariable("object") String object, HttpServletResponse response) throws MinioException, IOException { InputStream inputStream = minioService.get(Paths.get(object));
response.addHeader("Content-disposition", "attachment;filename=" + object); response.setContentType(URLConnection.guessContentTypeFromName(object)); IOUtils.copy(inputStream, response.getOutputStream()); response.flushBuffer(); }
@PostMapping("/upload") public void addAttachement(@RequestParam("file") MultipartFile file) { Path path = Paths.get(file.getOriginalFilename()); try { minioService.upload(path, file.getInputStream(), file.getContentType()); } catch (MinioException e) { throw new IllegalStateException("The file cannot be upload on the internal storage. Please retry later", e); } catch (IOException e) { throw new IllegalStateException("The file cannot be read", e); } } }
|
如果你觉得这个 MinioService功能不够强大,你可以注入原始SDK中的MinioClient。
1 2
| @Autowired private MinioClient minioClient;
|
测试接口
文件传接口
文件列表接口
获取文件
文中代码详见 springboot-minio