Java中Minio的基本使用詳解
1 Minio簡介
MinIO 是一個基于Apache License v2.0開源協(xié)議的對象存儲服務(wù)。
它兼容亞馬遜S3云存儲服務(wù)接口,非常適合于存儲大容量非結(jié)構(gòu)化的數(shù)據(jù),例如圖片、視頻、日志文件、備份數(shù)據(jù)和容器/虛擬機鏡像等,而一個對象文件可以是任意大小,從幾kb到最大5T不等。
1.1 支持非結(jié)構(gòu)化的數(shù)據(jù)存儲

1.2 分布式部署
分布式Minio可以讓你將多塊硬盤(甚至在不同的機器上)組成一個對象存儲服務(wù)。由于硬盤分布在不同的節(jié)點上,分布式Minio避免了單點故障。
在大數(shù)據(jù)領(lǐng)域,通常的設(shè)計理念都是無中心和分布式。Minio分布式模式可以幫助你搭建一個高可用的對象存儲服務(wù),你可以使用這些存儲設(shè)備,而不用考慮其真實物理位置。
Notes:分布式Minio至少需要4個硬盤,使用分布式Minio自動引入了糾刪碼功能。
2 使用docker安裝并啟動Minio服務(wù)
2.1 docker安裝minio
可以使用:docker search minio查看docker倉庫中的各個版本,可選擇裝指定版本
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中會使用。
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaWeb-WebSocket瀏覽器服務(wù)器雙向通信方式
文章介紹了WebSocket協(xié)議的工作原理和應(yīng)用場景,包括與HTTP的對比,接著,詳細(xì)介紹了如何在Java中使用WebSocket,包括配置類、服務(wù)編寫和前端頁面的實現(xiàn)2025-02-02
springmvc不進入Controller導(dǎo)致404的問題
這篇文章主要介紹了springmvc不進入Controller導(dǎo)致404的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
詳解如何用SpringBoot 2.3.0.M1創(chuàng)建Docker映像
這篇文章主要介紹了詳解如何用SpringBoot 2.3.0.M1創(chuàng)建Docker映像,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Spring Boot創(chuàng)建非可執(zhí)行jar包的實例教程
這篇文章主要介紹了Spring Boot創(chuàng)建非可執(zhí)行jar包的實例教程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
Eclipse Debug模式的開啟與關(guān)閉問題簡析
這篇文章主要介紹了Eclipse Debug模式的開啟與關(guān)閉問題簡析,同時向大家介紹了一個簡單的debug模式啟動不起來的解決方法,希望對大家有所幫助。2017-10-10
Java設(shè)計模式之橋接模式詳解(Bridge Pattern)
橋接模式是一種結(jié)構(gòu)型設(shè)計模式,旨在將抽象部分與其實現(xiàn)部分分離,從而使兩者可以獨立地變化,橋接模式通過組合關(guān)系代替繼承關(guān)系,將抽象和實現(xiàn)解耦,使代碼更具擴展性和維護性2025-02-02

