Minio簡介
Minio是Apache License v2.0下發(fā)布的對象存儲服務(wù)器馁启。它與Amazon S3云存儲服務(wù)兼容芍秆。它最適合存儲非結(jié)構(gòu)化數(shù)據(jù),如照片霉颠,視頻蒿偎,日志文件怀读,備份和容器/ VM映像。對象的大小可以從幾KB到最大5TB
Minio服務(wù)器足夠輕苍糠,可以與應(yīng)用程序堆棧捆綁在一起啤誊,類似于NodeJS蚊锹,Redis和MySQL。
Minio Docker安裝
1. docker search minio --查找鏡像
2. docker pull minio/minio --安裝鏡像
3. docker run -p 9018:9000 --name minio1
-e "MINIO_ACCESS_KEY=AKIAIOSFODNN7EXAMPLE"
-e "MINIO_SECRET_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"
-v /mnt/data:/data
-v /mnt/config:/root/.minio
minio/minio server /data
4.docker logs 容器Id --查看minio賬號密碼
**注:Minio的安裝具體步驟可以看這篇:http://www.reibang.com/p/68ac0477291d (推薦)
或者是 https://blog.csdn.net/L531003231/article/details/102969890?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.channel_param
**
Minio 整合SpringBoot 案例
ps:下面代碼出現(xiàn)的salt 是我自己創(chuàng)建的bucket
- Minio相關(guān)依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.minio/minio -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>6.0.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 配置文件增加Minio相關(guān)配置
minio:
endpoint: 自己的minio地址
accesskey: 賬號
secretkey: 密碼
3.Minio實(shí)體類
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioProp {
/**
連接地址
*/
private String endpoint;
/**
* 用戶名
*/
private String accesskey;
/**
* 密碼
*/
private String secretkey;
}
- Minio配置類
@Configuration
@EnableConfigurationProperties(MinioProp.class)
public class MinioConfig {
@Autowired
private MinioProp minioProp;
/**
* 獲取MinioClient
*/
@Bean
public MinioClient minioClient() throws InvalidPortException, InvalidEndpointException {
return new MinioClient(minioProp.getEndpoint(),minioProp.getAccesskey(),minioProp.getSecretkey());
}
}
5.Minio工具類(查詢bucket/上傳文件/獲取文件/刪除文件...)
@Component
public class MinioUtils {
@Autowired
private MinioProp minioProp;
@Autowired
private MinioClient client;
/**
* 創(chuàng)建bucket
*/
public void createBucket(String bucketName) throws Exception{
if (!client.bucketExists(bucketName)) {
client.makeBucket(bucketName);
}
}
/**
* 上傳文件
*/
public JSONObject uploadFile(MultipartFile file,String bucketName) throws Exception{
JSONObject res = new JSONObject();
res.put("code",0);
//判斷文件是否為空
if (null == file || 0 == file.getSize()) {
res.put("msg","上傳文件不能為空");
return res;
}
//判斷存儲桶是否存在 不存在則創(chuàng)建
createBucket(bucketName);
//文件名
String originalFilename = file.getOriginalFilename();
//新的文件名 = 存儲桶文件名_時間戳.后綴名
String filelName = bucketName + "_" +
System.currentTimeMillis() +
originalFilename.substring(originalFilename.lastIndexOf("."));
//開始上傳
client.putObject(bucketName,filelName,file.getInputStream(),file.getContentType());
res.put("code",1);
res.put("msg",minioProp.getEndpoint() + "/" + bucketName + "/" + filelName);
return res;
}
/**
* 獲取全部bucket
*/
public List<Bucket> getAllBuckets() throws Exception{
return client.listBuckets();
}
/**
* 根據(jù)bucketName獲取信息
*
* @param bucketName bucket名稱
*/
public Optional<Bucket> getBucket(String bucketName) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InsufficientDataException, InvalidResponseException, InternalException, NoResponseException, InvalidBucketNameException, XmlPullParserException, ErrorResponseException {
return client.listBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
}
/**
* 根據(jù)bucketName刪除信息
*
* @param bucketName bucket名稱
*/
public void removeBucket(String bucketName) throws Exception{
client.removeBucket(bucketName);
}
/**
* 獲取?件外鏈
*
* @param bucketName bucket名稱
* @param objectName ?件名稱
* @param expires 過期時間 <=7
* @return url
*/
public String getObjectURL(String bucketName, String objectName, Integer expires) throws Exception{
return client.presignedGetObject(bucketName, objectName, expires);
}
/**
* 獲取?件
*
* @param bucketName bucket名稱
* @param objectName ?件名稱
* @return ?進(jìn)制流
*/
public InputStream getObject(String bucketName, String objectName) throws Exception{
return client.getObject(bucketName, objectName);
}
/**
* 上傳?件
*
* @param bucketName bucket名稱
* @param objectName ?件名稱
* @param stream ?件流
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
*/
public void putObject(String bucketName, String objectName, InputStream stream) throws
Exception {
client.putObject(bucketName, objectName, stream, stream.available(),
"application/octet-stream");
}
/**
* 上傳?件
*
* @param bucketName bucket名稱
* @param objectName ?件名稱
* @param stream ?件流
* @param size ??
* @param contextType 類型
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#putObject
*/
public void putObject(String bucketName, String objectName, InputStream stream, long
size, String contextType) throws Exception {
client.putObject(bucketName, objectName, stream, size, contextType);
}
/**
* 獲取?件信息
*
* @param bucketName bucket名稱
* @param objectName ?件名稱
* @throws Exception https://docs.minio.io/cn/java-client-api-reference.html#statObject
*/
public ObjectStat getObjectInfo(String bucketName, String objectName) throws Exception
{
return client.statObject(bucketName, objectName);
}
/**
* 刪除?件
*
* @param bucketName bucket名稱
* @param objectName ?件名稱
* @throws Exception https://docs.minio.io/cn/java-client-apireference.html#removeObject
*/
public void removeObject(String bucketName, String objectName) throws Exception {
client.removeObject(bucketName, objectName);
}
}
6.Controller類
@Controller
public class MinioController {
@Autowired
private MinioUtils minioUtils;
/**
* 初始化頁面
*/
@GetMapping("init")
public String init(){
return "file";
}
/**
* 上傳文件
*/
@PostMapping("/upload")
public void upload(@RequestParam(name = "file",required = false)MultipartFile file){
JSONObject res = null;
try {
res = minioUtils.uploadFile(file,"salt");
}catch (Exception e){
e.printStackTrace();
res.put("code",0);
res.put("msg","上傳失敗" + e.getMessage());
}
System.out.println(res.toString());
}
/**
* 刪除文件
* @param objectName
* @throws Exception
*/
@RequestMapping("/delete/{objectName}")
public void delete(@PathVariable("objectName") String objectName) throws Exception{
minioUtils.removeObject("salt",objectName);
System.out.println("刪除成功");
}
/**
* 下載文件到本地
* @param objectName
* @param response
* @return
* @throws Exception
*/
@GetMapping("/download/{objectName}")
public ResponseEntity download2(@PathVariable("objectName") String objectName, HttpServletResponse response) throws Exception{
ResponseEntity<byte[]> responseEntity = null;
InputStream stream = null;
ByteArrayOutputStream output = null;
try {
// 獲取"myobject"的輸入流捷沸。
stream = minioUtils.getObject("salt", objectName);
if (stream == null) {
System.out.println("文件不存在");
}
//用于轉(zhuǎn)換byte
output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = stream.read(buffer))) {
output.write(buffer, 0, n);
}
byte[] bytes = output.toByteArray();
//設(shè)置header
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Accept-Ranges", "bytes");
httpHeaders.add("Content-Length", bytes.length + "");
// objectName = new String(objectName.getBytes("UTF-8"), "ISO8859-1");
//把文件名按UTF-8取出并按ISO8859-1編碼痒给,保證彈出窗口中的文件名中文不亂碼苍柏,中文不要太多,最多支持17個中文试吁,因?yàn)閔eader有150個字節(jié)限制。
httpHeaders.add("Content-disposition", "attachment; filename=" + objectName);
httpHeaders.add("Content-Type", "text/plain;charset=utf-8");
// httpHeaders.add("Content-Type", "image/jpeg");
responseEntity = new ResponseEntity<byte[]>(bytes, httpHeaders, HttpStatus.CREATED);
} catch (MinioException e) {
e.printStackTrace();
} finally {
if (stream != null) {
stream.close();
}
if (output != null) {
output.close();
}
}
return responseEntity;
}
/**
* 在瀏覽器預(yù)覽圖片
* @param objectName
* @param response
* @throws Exception
*/
@RequestMapping("/preViewPicture/{objectName}")
public void download1(@PathVariable("objectName") String objectName, HttpServletResponse response) throws Exception{
response.setContentType("image/jpeg");
ServletOutputStream out = null;
try {
out = response.getOutputStream();
InputStream stream = minioUtils.getObject("salt",objectName);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = stream.read(buffer))) {
output.write(buffer, 0, n);
}
byte[] bytes = output.toByteArray();
out.write(bytes == null ? new byte[0]:bytes);
out.flush();
}finally {
if (out != null) {
out.close();
}
}
}
}
- 簡單頁面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>測試minio上傳</title>
</head>
<body>
<form th:action="@{/upload}" accept-charset="UTF-8" method="post" enctype="multipart/form-data" target="_blank">
文件:<input type="file" name="file">
<input type="submit" value="上傳">
</form>
</body>
</html>
前面工具類可以參考:https://www.bilibili.com/read/cv6014961
經(jīng)過測試余耽,上傳,刪除碟贾,下載都已實(shí)現(xiàn)