版本選擇
- JDK 1.8
1. CentOS安裝MinIO
https://blog.csdn.net/llwy1428/article/details/99618252
2. 創建Bucket
在MinIO控制台中創建Bucket:springboot-minio-demo
3. 創建Maven項目
3.1. 項目結構
3.2. pom.xml引入依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>springboot-minio-demo</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>7.0.2</version>
</dependency>
</dependencies>
</project>
3.3. application.yaml 配置MinIO
server:
port: 8080
tomcat:
connection-timeout: 10000 # 10s
minio:
endpoint: 192.168.0.121
port: 9000
accessKey: minioadmin
secretKey: minioadmin
secure: false
bucketName: springboot-minio-dev
3.4. SpringBoot啟動類
@SpringBootApplication(scanBasePackages = "com.demo.*")
public class MinioDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MinioDemoApplication.class, args);
}
}
3.5. MinioProperties
獲取yaml中的minio配置信息
package com.demo.entity;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* MinIO配置獲取
* @author ch3nw3i@gitee
*/
@ConfigurationProperties(prefix = "minio")
@Component
@Data
public class MinioProperties {
private String endpoint;
private Integer port;
private String accessKey;
private String secretKey;
private Boolean secure;
private String bucketName;
}
3.6. Minio配置類
package com.demo.config;
import com.demo.entity.MinioProperties;
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
* MinIO配置類
* @author ch3nw3i@gitee
*/
@Data
@Component
public class MinioConfig {
@Autowired
private MinioProperties minioProperties;
@Bean
public MinioClient getMinioClient() throws InvalidEndpointException, InvalidPortException {
MinioClient minioClient = new MinioClient(minioProperties.getEndpoint(), minioProperties.getPort(),
minioProperties.getAccessKey(), minioProperties.getSecretKey(), minioProperties.getSecure());
return minioClient;
}
}
3.7. MinioUtils工具類
package com.demo.utils;
import io.minio.MinioClient;
import io.minio.ObjectStat;
import io.minio.PutObjectOptions;
import io.minio.Result;
import io.minio.errors.ErrorResponseException;
import io.minio.errors.InvalidExpiresRangeException;
import io.minio.messages.Bucket;
import io.minio.messages.DeleteError;
import io.minio.messages.Item;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
/**
* MinIO工具類
* @author ch3nw3i@gitee
*/
@Component
public class MinioUtils {
@Autowired
private MinioClient minioClient;
private static final int MIN_MULTIPART_SIZE = 0;
private static final int DEFAULT_EXPIRY_TIME = 7 * 24 * 3600;
/**
* 檢查存儲桶是否存在
*
* @param bucketName 存儲桶名稱
* @return
*/
@SneakyThrows
public boolean bucketExists(String bucketName) {
boolean flag = false;
flag = minioClient.bucketExists(bucketName);
if (flag) {
return true;
}
return false;
}
/**
* 創建存儲桶
*
* @param bucketName 存儲桶名稱
*/
@SneakyThrows
public boolean makeBucket(String bucketName) {
boolean flag = bucketExists(bucketName);
if (!flag) {
minioClient.makeBucket(bucketName);
return true;
} else {
return false;
}
}
/**
* 列出所有存儲桶名稱
*
* @return
*/
@SneakyThrows
public List<String> listBucketNames() {
List<Bucket> bucketList = listBuckets();
List<String> bucketListName = new ArrayList<>();
for (Bucket bucket : bucketList) {
bucketListName.add(bucket.name());
}
return bucketListName;
}
/**
* 列出所有存儲桶
*
* @return
*/
@SneakyThrows
public List<Bucket> listBuckets() {
return minioClient.listBuckets();
}
/**
* 刪除存儲桶
*
* @param bucketName 存儲桶名稱
* @return
*/
@SneakyThrows
public boolean removeBucket(String bucketName) {
boolean flag = bucketExists(bucketName);
if (flag) {
Iterable<Result<Item>> myObjects = listObjects(bucketName);
for (Result<Item> result : myObjects) {
Item item = result.get();
// 有對象文件,則刪除失敗
if (item.size() > 0) {
return false;
}
}
// 刪除存儲桶,注意,只有存儲桶為空時才能刪除成功。
minioClient.removeBucket(bucketName);
flag = bucketExists(bucketName);
if (!flag) {
return true;
}
}
return false;
}
/**
* 列出存儲桶中的所有對象名稱
*
* @param bucketName 存儲桶名稱
* @return
*/
@SneakyThrows
public List<String> listObjectNames(String bucketName) {
List<String> listObjectNames = new ArrayList<>();
boolean flag = bucketExists(bucketName);
if (flag) {
Iterable<Result<Item>> myObjects = listObjects(bucketName);
for (Result<Item> result : myObjects) {
Item item = result.get();
listObjectNames.add(item.objectName());
}
}
return listObjectNames;
}
/**
* 列出存儲桶中的所有對象
*
* @param bucketName 存儲桶名稱
* @return
*/
@SneakyThrows
public Iterable<Result<Item>> listObjects(String bucketName) {
boolean flag = bucketExists(bucketName);
if (flag) {
return minioClient.listObjects(bucketName);
}
return null;
}
/**
* 通過文件上傳到對象
*
* @param bucketName 存儲桶名稱
* @param objectName 存儲桶里的對象名稱
* @param fileName File name
* @return
*/
@SneakyThrows
public boolean putObject(String bucketName, String objectName, String fileName) {
boolean flag = bucketExists(bucketName);
if (flag) {
minioClient.putObject(bucketName, objectName, fileName, null);
ObjectStat statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
return true;
}
}
return false;
}
/**
* 文件上傳
*
* @param bucketName
* @param multipartFile
*/
@SneakyThrows
public void putObject(String bucketName, MultipartFile multipartFile, String filename) {
PutObjectOptions putObjectOptions = new PutObjectOptions(multipartFile.getSize(), MinioUtils.MIN_MULTIPART_SIZE);
putObjectOptions.setContentType(multipartFile.getContentType());
minioClient.putObject(bucketName, filename, multipartFile.getInputStream(), putObjectOptions);
}
/**
* 通過InputStream上傳對象
*
* @param bucketName 存儲桶名稱
* @param objectName 存儲桶里的對象名稱
* @param stream 要上傳的流
* @return
*/
@SneakyThrows
public boolean putObject(String bucketName, String objectName, InputStream stream) {
boolean flag = bucketExists(bucketName);
if (flag) {
minioClient.putObject(bucketName, objectName, stream, new PutObjectOptions(stream.available(), -1));
ObjectStat statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
return true;
}
}
return false;
}
/**
* 以流的形式獲取一個文件對象
*
* @param bucketName 存儲桶名稱
* @param objectName 存儲桶里的對象名稱
* @return
*/
@SneakyThrows
public InputStream getObject(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
if (flag) {
ObjectStat statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
InputStream stream = minioClient.getObject(bucketName, objectName);
return stream;
}
}
return null;
}
/**
* 以流的形式獲取一個文件對象(斷點下載)
*
* @param bucketName 存儲桶名稱
* @param objectName 存儲桶里的對象名稱
* @param offset 起始字節的位置
* @param length 要讀取的長度 (可選,如果無值則代表讀到文件結尾)
* @return
*/
@SneakyThrows
public InputStream getObject(String bucketName, String objectName, long offset, Long length) {
boolean flag = bucketExists(bucketName);
if (flag) {
ObjectStat statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
InputStream stream = minioClient.getObject(bucketName, objectName, offset, length);
return stream;
}
}
return null;
}
/**
* 下載並將文件保存到本地
*
* @param bucketName 存儲桶名稱
* @param objectName 存儲桶里的對象名稱
* @param fileName File name
* @return
*/
@SneakyThrows
public boolean getObject(String bucketName, String objectName, String fileName) {
boolean flag = bucketExists(bucketName);
if (flag) {
ObjectStat statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.length() > 0) {
minioClient.getObject(bucketName, objectName, fileName);
return true;
}
}
return false;
}
/**
* 刪除一個對象
*
* @param bucketName 存儲桶名稱
* @param objectName 存儲桶里的對象名稱
*/
@SneakyThrows
public boolean removeObject(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
if (flag) {
minioClient.removeObject(bucketName, objectName);
return true;
}
return false;
}
/**
* 刪除指定桶的多個文件對象,返回刪除錯誤的對象列表,全部刪除成功,返回空列表
*
* @param bucketName 存儲桶名稱
* @param objectNames 含有要刪除的多個object名稱的迭代器對象
* @return
*/
@SneakyThrows
public List<String> removeObject(String bucketName, List<String> objectNames) {
List<String> deleteErrorNames = new ArrayList<>();
boolean flag = bucketExists(bucketName);
if (flag) {
Iterable<Result<DeleteError>> results = minioClient.removeObjects(bucketName, objectNames);
for (Result<DeleteError> result : results) {
DeleteError error = result.get();
deleteErrorNames.add(error.objectName());
}
}
return deleteErrorNames;
}
/**
* 生成一個給HTTP GET請求用的presigned URL。
* 瀏覽器/移動端的客戶端可以用這個URL進行下載,即使其所在的存儲桶是私有的。這個presigned URL可以設置一個失效時間,默認值是7天。
*
* @param bucketName 存儲桶名稱
* @param objectName 存儲桶里的對象名稱
* @param expires 失效時間(以秒為單位),默認是7天,不得大於七天
* @return
*/
@SneakyThrows
public String presignedGetObject(String bucketName, String objectName, Integer expires) {
boolean flag = bucketExists(bucketName);
String url = "";
if (flag) {
if (expires < 1 || expires > DEFAULT_EXPIRY_TIME) {
throw new InvalidExpiresRangeException(expires,
"expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
}
url = minioClient.presignedGetObject(bucketName, objectName, expires);
}
return url;
}
/**
* 生成一個給HTTP PUT請求用的presigned URL。
* 瀏覽器/移動端的客戶端可以用這個URL進行上傳,即使其所在的存儲桶是私有的。這個presigned URL可以設置一個失效時間,默認值是7天。
*
* @param bucketName 存儲桶名稱
* @param objectName 存儲桶里的對象名稱
* @param expires 失效時間(以秒為單位),默認是7天,不得大於七天
* @return
*/
@SneakyThrows
public String presignedPutObject(String bucketName, String objectName, Integer expires) {
boolean flag = bucketExists(bucketName);
String url = "";
if (flag) {
if (expires < 1 || expires > DEFAULT_EXPIRY_TIME) {
throw new InvalidExpiresRangeException(expires,
"expires must be in range of 1 to " + DEFAULT_EXPIRY_TIME);
}
url = minioClient.presignedPutObject(bucketName, objectName, expires);
}
return url;
}
/**
* 獲取對象的元數據
*
* @param bucketName 存儲桶名稱
* @param objectName 存儲桶里的對象名稱
* @return
*/
@SneakyThrows
public ObjectStat statObject(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
if (flag) {
ObjectStat statObject = minioClient.statObject(bucketName, objectName);
return statObject;
}
return null;
}
/**
* 文件訪問路徑
*
* @param bucketName 存儲桶名稱
* @param objectName 存儲桶里的對象名稱
* @return
*/
@SneakyThrows
public String getObjectUrl(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
String url = "";
if (flag) {
url = minioClient.getObjectUrl(bucketName, objectName);
}
return url;
}
public void downloadFile(String bucketName, String fileName, String originalName, HttpServletResponse response) {
try {
InputStream file = minioClient.getObject(bucketName, fileName);
String filename = new String(fileName.getBytes("ISO8859-1"), StandardCharsets.UTF_8);
if (StringUtils.isNotEmpty(originalName)) {
fileName = originalName;
}
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
ServletOutputStream servletOutputStream = response.getOutputStream();
int len;
byte[] buffer = new byte[1024];
while ((len = file.read(buffer)) > 0) {
servletOutputStream.write(buffer, 0, len);
}
servletOutputStream.flush();
file.close();
servletOutputStream.close();
} catch (ErrorResponseException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
3.8. OssController文件上傳接口
package com.demo.controller;
import com.demo.entity.MinioProperties;
import com.demo.utils.MinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
* @author ch3nw3i@gitee
*/
@RestController
@RequestMapping("")
public class OssController {
@Autowired
private MinioProperties minioProperties;
@Autowired
private MinioUtils minioUtils;
@PostMapping(value = "/uploadFile")
public String uploadFile(@RequestBody MultipartFile multipartFile) {
String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String newFilename = UUID.randomUUID().toString();
String suffix = multipartFile.getOriginalFilename().split("\\.")[1];
minioUtils.putObject(minioProperties.getBucketName(), multipartFile, date + "/" + newFilename + "." + suffix);
String url = "http://" + minioProperties.getEndpoint() + ":" + minioProperties.getPort() + "/" +
minioProperties.getBucketName() + "/" + date + "/" + newFilename +
"." + suffix;
return url;
}
}
4. Postman測試
將接口返回的URL復制到瀏覽器中即可查看。
附:Demo下載
Gitee克隆地址:https://gitee.com/ch3nw3i/springboot-minio-demo.git