您好,登錄后才能下訂單哦!
這篇文章主要介紹“SpringBoot如何整合minio”的相關(guān)知識(shí),小編通過(guò)實(shí)際案例向大家展示操作過(guò)程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“SpringBoot如何整合minio”文章能幫助大家解決問(wèn)題。
首先添加Minio的依賴(lài)
<dependency> <groupId>io.minio</groupId> <artifactId>minio</artifactId> <version>3.0.10</version> </dependency>
然后寫(xiě)一個(gè)controller類(lèi)
這只是一個(gè)簡(jiǎn)單的demo,沒(méi)有進(jìn)行任何的封裝,可以根據(jù)實(shí)際情況進(jìn)行封裝。
package com.file.server.controller; import io.minio.MinioClient; import org.apache.tomcat.util.http.fileupload.IOUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.InputStream; @RestController public class MinioController { private static String url = "http://127.0.0.1:9000"; //minio服務(wù)的IP端口 private static String accessKey = "W2ZWITFFDWFM5TWS3WI9"; private static String secretKey = "dNx++XsRJpjmWVQHWv8djMCFJ0A3YXbEr4qfKHR+"; //上傳文件到minio服務(wù) @PostMapping("upload") public String upload(@RequestParam("fileName") MultipartFile file ) { try { MinioClient minioClient = new MinioClient(url, accessKey, secretKey); InputStream is= file.getInputStream(); //得到文件流 String fileName = file.getOriginalFilename(); //文件名 String contentType = file.getContentType(); //類(lèi)型 minioClient.putObject("file",fileName,is,contentType); //把文件放置Minio桶(文件夾) return "上傳成功"; }catch (Exception e){ return "上傳失敗"; } } //下載minio服務(wù)的文件 @GetMapping("download") public String download(HttpServletResponse response){ try { MinioClient minioClient = new MinioClient(url, accessKey, secretKey); InputStream fileInputStream = minioClient.getObject("file", "test.jpg"); response.setHeader("Content-Disposition", "attachment;filename=" + "test.jpg"); response.setContentType("application/force-download"); response.setCharacterEncoding("UTF-8"); IOUtils.copy(fileInputStream,response.getOutputStream()); return "下載完成"; }catch (Exception e){ return "下載失敗"; } } //獲取minio文件的下載地址 @GetMapping("url") public String getUrl(){ try { MinioClient minioClient = new MinioClient(url, accessKey, secretKey); String url = minioClient.presignedGetObject("file", "test.jpg"); return url; }catch (Exception e){ return "獲取失敗"; } } }
關(guān)于“SpringBoot如何整合minio”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。