對象存儲服務(wù)MinIO快速入門(集成項目的詳細(xì)過程)
對象存儲服務(wù)MinIO
MinIO簡介
MinIO基于Apache License v2.0開源協(xié)議的對象存儲服務(wù),可以做為云存儲的解決方案用來保存海量的圖片,視頻,文檔。由于采用Golang實現(xiàn),服務(wù)端可以工作在Windows,Linux, OS X和FreeBSD上。配置簡單,基本是復(fù)制可執(zhí)行程序,單行命令可以運(yùn)行起來。
MinIO特點(diǎn)
- 數(shù)據(jù)保護(hù)
Minio使用Minio Erasure Code(糾刪碼)來防止硬件故障。即便損壞一半以上的driver,但是仍然可以從中恢復(fù)。
- 高性能
作為高性能對象存儲,在標(biāo)準(zhǔn)硬件條件下它能達(dá)到55GB/s的讀、35GB/s的寫速率
- 可擴(kuò)容
不同MinIO集群可以組成聯(lián)邦,并形成一個全局的命名空間,并跨越多個數(shù)據(jù)中心
- SDK支持
基于Minio輕量的特點(diǎn),它得到類似Java、Python或Go等語言的sdk支持
- 有操作頁面
面向用戶友好的簡單操作界面,非常方便的管理Bucket及里面的文件資源
- 功能簡單
這一設(shè)計原則讓MinIO不容易出錯、更快啟動
- 豐富的API
支持文件資源的分享連接及分享鏈接的過期策略、存儲桶操作、文件列表訪問及文件上傳下載的基本功能等。
- 文件變化主動通知
存儲桶(Bucket)如果發(fā)生改變,比如上傳對象和刪除對象,可以使用存儲桶事件通知機(jī)制進(jìn)行監(jiān)控,并通過以下方式發(fā)布出去:AMQP、MQTT、Elasticsearch、Redis、NATS、MySQL、Kafka、Webhooks等。
一、安裝啟動
我采用的是docker安裝
docker pull minio/minio
創(chuàng)建配置文件目錄和上傳目錄
mkdir -p /home/minio/configmkdir -p /home/minio/data
啟動容器
docker run -p 9000:9000 -p 9090:9090 \
--net=host \
--name minio \
-d --restart=always \
-e "MINIO_ACCESS_KEY=admin" \
-e "MINIO_SECRET_KEY=admin123456" \
-v /home/minio/data:/data \
-v /home/minio/config:/root/.minio \
minio/minio server \
/data --console-address ":9090" -address ":9000"假設(shè)我們的服務(wù)器地址為http://192.168.200.100:9000,我們在地址欄輸入:http://http://192.168.200.100:9000/ 即可進(jìn)入登錄界面。

Access Key為admin ,Secret_key 為admin123456進(jìn)入系統(tǒng)后可以看到主界面

一、快速入門
創(chuàng)建springboot項目
1 導(dǎo)入依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>在配置文件中導(dǎo)入
minio: accessKey: admin secretKey: admin123456 bucket: text endpoint: http://192.168.200.100:9000 readPath: http://192.168.200.100:9000
2 配置類
MinIOProperties
@Configuration
@Data
@ConfigurationProperties(prefix = "minio")
public class MinIOProperties {
private String accessKey;
private String secretKey;
private String bucket;
private String endpoint;
private String readPath;
}MinIOClientConfig
@Data
@Primary
@Configuration
public class MinIOClientConfig {
@Autowired
private MinIOProperties minIOProperties;
@Bean
public MinioClient buildMinioClient(){
return MinioClient
.builder()
.credentials(minIOProperties.getAccessKey(), minIOProperties.getSecretKey())
.endpoint(minIOProperties.getEndpoint())
.build();
}
}3 開始業(yè)務(wù)
Controller
@RestController
@RequestMapping("minio")
public class UploadController {
@Autowired
private FileService fileService;
@PostMapping("/upload")
public String uploadMinio(@RequestPart("file") MultipartFile file) {
return fileService.uploadMinio(file);
}
}Service
public interface FileService {
public String uploadMinio(MultipartFile file) ;
}ServiceImpl
@Slf4j
@Service
public class FileServiceImpl implements FileService {
@Autowired
private MinioClient minioClient;
@Autowired
private MinIOProperties minioProperties;
@Override
public String uploadMinio(MultipartFile file) {
System.out.println(minioProperties);
try {
// 判斷桶是否存在
boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucket()).build());
if (!found) { // 如果不存在,那么此時就創(chuàng)建一個新的桶
minioClient.makeBucket(MakeBucketArgs.builder().bucket(minioProperties.getBucket()).build());
} else { // 如果存在打印信息
System.out.println("Bucket 'daijia' already exists.");
}
// 設(shè)置存儲對象名稱
String extFileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
String fileName = new SimpleDateFormat("yyyyMMdd")
.format(new Date()) + "/" + UUID.randomUUID().toString().replace("-" , "") + "." + extFileName;
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.bucket(minioProperties.getBucket())
.stream(file.getInputStream(), file.getSize(), -1)
.object(fileName)
.build();
minioClient.putObject(putObjectArgs) ;
return minioProperties.getEndpoint() + "/" + minioProperties.getBucket() + "/" + fileName ;
} catch (Exception e) {
e.printStackTrace();
}
return "error";
}
}4 測試

點(diǎn)擊連接,就可以開始下載圖片
如果發(fā)現(xiàn)圖片報錯,去控制臺把Access Policy 改成Pubilc

完成。
不用web的方式也可以上傳圖片:
Service
public interface FileStorageService {
/**
* 上傳圖片文件
* @param prefix 文件前綴
* @param filename 文件名
* @param inputStream 文件流
* @return 文件全路徑
*/
public String uploadImgFile(String prefix, String filename,InputStream inputStream);
/**
* 刪除文件
* @param pathUrl 文件全路徑
*/
public void delete(String pathUrl);
}ServiceImpl
@Slf4j
@Service
public class MinIOFileStorageServiceImpl implements FileStorageService {
@Autowired
private MinioClient minioClient;
@Autowired
private MinIOProperties minIOConfigProperties;
private final static String separator = "/";
/**
* 上傳圖片文件
* @param prefix 文件前綴
* @param filename 文件名
* @param inputStream 文件流
* @return 文件全路徑
*/
@Override
public String uploadImgFile(String prefix, String filename, InputStream inputStream) {
String filePath = builderFilePath(prefix, filename);
try {
PutObjectArgs putObjectArgs = PutObjectArgs.builder()
.object(filePath)
.contentType("image/jpg")
.bucket(minIOConfigProperties.getBucket()).stream(inputStream,inputStream.available(),-1)
.build();
minioClient.putObject(putObjectArgs);
StringBuilder urlPath = new StringBuilder(minIOConfigProperties.getReadPath());
urlPath.append(separator+minIOConfigProperties.getBucket());
urlPath.append(separator);
urlPath.append(filePath);
return urlPath.toString();
}catch (Exception ex){
log.error("minio put file error.",ex);
throw new RuntimeException("上傳文件失敗");
}
}
/**
* 刪除文件
* @param pathUrl 文件全路徑
*/
@Override
public void delete(String pathUrl) {
String key = pathUrl.replace(minIOConfigProperties.getEndpoint()+"/","");
int index = key.indexOf(separator);
String bucket = key.substring(0,index);
String filePath = key.substring(index+1);
// 刪除Objects
RemoveObjectArgs removeObjectArgs = RemoveObjectArgs.builder().bucket(bucket).object(filePath).build();
try {
minioClient.removeObject(removeObjectArgs);
} catch (Exception e) {
log.error("minio remove file error. pathUrl:{}",pathUrl);
e.printStackTrace();
}
}
}測試類:
@SpringBootTest
class MinioDemoApplicationTests {
@Autowired
private MinIOFileStorageServiceImpl minIOFileStorageServiceImpl;
@Test
void imageFileStorage() throws IOException {
String fileName = UUID.randomUUID().toString().replaceAll("-", "");
String path = minIOFileStorageServiceImpl.uploadImgFile("text", fileName, Files.newInputStream(new File("D:\\圖片\\1.jpg").toPath()));
System.out.println(path);
}
@Test
void deleteImageFile() throws IOException {
minIOFileStorageServiceImpl.delete("");//這里填上傳的文件路徑
}
}完成。
到此這篇關(guān)于對象存儲服務(wù)MinIO快速入門(集成項目的詳細(xì)過程)的文章就介紹到這了,更多相關(guān)對象存儲服務(wù)MinIO內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java策略枚舉:消除在項目里大批量使用if-else的優(yōu)雅姿勢
這篇文章主要給大家介紹了關(guān)于Java徹底消滅if-else的8種方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2021-06-06
springboot配置多數(shù)據(jù)源的一款框架(dynamic-datasource-spring-boot-starter
dynamic-datasource-spring-boot-starter 是一個基于 springboot 的快速集成多數(shù)據(jù)源的啟動器,今天通過本文給大家分享這款框架配置springboot多數(shù)據(jù)源的方法,一起看看吧2021-09-09
JavaWeb如何實現(xiàn)統(tǒng)一查詢接口(jfinal)
這篇文章主要介紹了JavaWeb如何實現(xiàn)統(tǒng)一查詢接口(jfinal),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
springboot多環(huán)境配置方案(不用5分鐘)
這篇文章主要介紹了springboot多環(huán)境配置方案(不用5分鐘),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
關(guān)于JDK+Tomcat+eclipse+MyEclipse的配置方法,看這篇夠了
關(guān)于JDK+Tomcat+eclipse+MyEclipse的配置問題,很多朋友都搞不太明白,網(wǎng)上一搜配置方法多種哪種最精簡呢,今天小編給大家分享一篇文章幫助大家快速掌握J(rèn)DK Tomcat eclipse MyEclipse配置技巧,需要的朋友參考下吧2021-06-06

