Java中Minio的基本使用詳解
1 Minio簡介
MinIO 是一個(gè)基于Apache License v2.0開源協(xié)議的對(duì)象存儲(chǔ)服務(wù)。
它兼容亞馬遜S3云存儲(chǔ)服務(wù)接口,非常適合于存儲(chǔ)大容量非結(jié)構(gòu)化的數(shù)據(jù),例如圖片、視頻、日志文件、備份數(shù)據(jù)和容器/虛擬機(jī)鏡像等,而一個(gè)對(duì)象文件可以是任意大小,從幾kb到最大5T不等。
1.1 支持非結(jié)構(gòu)化的數(shù)據(jù)存儲(chǔ)
1.2 分布式部署
分布式Minio可以讓你將多塊硬盤(甚至在不同的機(jī)器上)組成一個(gè)對(duì)象存儲(chǔ)服務(wù)。由于硬盤分布在不同的節(jié)點(diǎn)上,分布式Minio避免了單點(diǎn)故障。
在大數(shù)據(jù)領(lǐng)域,通常的設(shè)計(jì)理念都是無中心和分布式。Minio分布式模式可以幫助你搭建一個(gè)高可用的對(duì)象存儲(chǔ)服務(wù),你可以使用這些存儲(chǔ)設(shè)備,而不用考慮其真實(shí)物理位置。
Notes:分布式Minio至少需要4個(gè)硬盤,使用分布式Minio自動(dòng)引入了糾刪碼功能。
2 使用docker安裝并啟動(dòng)Minio服務(wù)
2.1 docker安裝minio
可以使用:docker search minio查看docker倉庫中的各個(gè)版本,可選擇裝指定版本
docker pull minio/minio
docker run -p 9000:9000 minio/minio server /data
安裝后使用瀏覽器訪問//127.0.0.1:9000,如果可以訪問,則表示minio已經(jīng)安裝成功。登錄名和密碼默認(rèn):minioadmin
創(chuàng)建bucket test,在springboot中會(huì)使用。
3 Springboot 中操作minio
3.1 添加操作依賴
<dependency> <groupId>com.jlefebure</groupId> <artifactId>spring-boot-starter-minio</artifactId> <version>1.1</version> </dependency>
3.2 配置全局參數(shù)
# Minio Host spring.minio.url=http://10.21.80.17:9000/ # Minio Bucket name for your application spring.minio.bucket=test # Minio access key (login) spring.minio.access-key=minioadmin # Minio secret key (password) spring.minio.secret-key=minioadmin ## MULTIPART (MultipartProperties) # Enable multipart uploads spring.servlet.multipart.enabled=true # Threshold after which files are written to disk. spring.servlet.multipart.file-size-threshold=2KB # Max file size. spring.servlet.multipart.max-file-size=500MB # Max Request Size spring.servlet.multipart.max-request-size=1024MB
3.3 測試案例
import com.jlefebure.spring.boot.minio.MinioConfigurationProperties; import com.jlefebure.spring.boot.minio.MinioException; import com.jlefebure.spring.boot.minio.MinioService; import io.minio.messages.Item; import org.apache.tomcat.util.http.fileupload.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.InputStreamResource; 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.net.URLConnection; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; @RestController public class MinioController { @Autowired private MinioService minioService; @Autowired private MinioConfigurationProperties configurationProperties; @GetMapping("/files") public List<Item> testMinio() throws MinioException { return minioService.list(); } @GetMapping("files/{object}") public void getObject(@PathVariable("object") String object, HttpServletResponse response) throws com.jlefebure.spring.boot.minio.MinioException, IOException { InputStream inputStream = minioService.get(Paths.get(object)); InputStreamResource inputStreamResource = new InputStreamResource(inputStream); // Set the content type and attachment header. response.addHeader("Content-disposition", "attachment;filename=" + object); response.setContentType(URLConnection.guessContentTypeFromName(object)); // Copy the stream to the response's output stream. IOUtils.copy(inputStream, response.getOutputStream()); response.flushBuffer(); } @PostMapping public void addAttachement(@RequestParam("file") MultipartFile file) throws IOException { System.out.println(file); String filename = file.getOriginalFilename(); Path path = Paths.get("/png/"+filename); String url = configurationProperties.getUrl() + "/" + configurationProperties.getBucket() + path.toAbsolutePath(); System.out.println(url); 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); } } }
到此這篇關(guān)于Java中Minio的基本使用詳解的文章就介紹到這了,更多相關(guān)Minio的基本使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
教你怎么用java一鍵自動(dòng)生成數(shù)據(jù)庫文檔
最近小編也在找這樣的插件,就是不想寫文檔了,浪費(fèi)時(shí)間和心情啊,果然我找到一款比較好用,操作簡單不復(fù)雜.screw 是一個(gè)簡潔好用的數(shù)據(jù)庫表結(jié)構(gòu)文檔的生成工具,支持 MySQL、Oracle、PostgreSQL 等主流的關(guān)系數(shù)據(jù)庫.需要的朋友可以參考下2021-05-05Spring Boot 約定大于配置之如何實(shí)現(xiàn)自定義配置
本文介紹了SpringBoot的“約定大于配置”理念以及如何實(shí)現(xiàn)自定義配置,通過實(shí)現(xiàn)接口和添加@Configuration注解,開發(fā)者可以靈活地?cái)U(kuò)展和定制SpringBoot的默認(rèn)行為,感興趣的朋友一起看看吧2025-03-03Java import static及import原理區(qū)別解析
這篇文章主要介紹了Java import static及import原理區(qū)別解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Spring配置數(shù)據(jù)源的三種方式(小結(jié))
本文主要介紹了Spring配置數(shù)據(jù)源的三種方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01spring?security?自定義Provider?如何實(shí)現(xiàn)多種認(rèn)證
這篇文章主要介紹了spring?security?自定義Provider實(shí)現(xiàn)多種認(rèn)證方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12IDEA Error:java: 無效的源發(fā)行版: 17錯(cuò)誤
本文主要介紹了IDEA Error:java: 無效的源發(fā)行版: 17錯(cuò)誤,這個(gè)錯(cuò)誤是因?yàn)槟腎DEA編譯器不支持Java 17版本,您需要更新您的IDEA編譯器或者將您的Java版本降級(jí)到IDEA支持的版本,本文就來詳細(xì)的介紹一下2023-08-08SpringBoot集成Kafka的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot集成Kafka的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01