MinIO是一个开源的对象存储服务器,它兼容Amazon S3服务接口,并具有高性能和可扩展性。MinIO支持在私有云环境中搭建自己的对象存储服务,同时也可以与公共云服务进行集成。
创建
创建一个桶
设置桶为public
创建密钥
引入依赖
java
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.22</version>
</dependency>
添加配置
minio.endpoint=http://192.168.2.4
minio.port=9000
minio.accessKey=mytest123
minio.secretKey=mytest123
minio.secure=false
minio.bucket-name=test
minio.image-size=10485760
minio.file-size=1073741824
配置类
java
import io.minio.MinioClient;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioProperties {
/**
* 是一个URL,域名,IPv4或者IPv6地址")
*/
private String endpoint;
/**
* //"TCP/IP端口号"
*/
private Integer port;
/**
* //"accessKey类似于用户ID,用于唯一标识你的账户"
*/
private String accessKey;
/**
* //"secretKey是你账户的密码"
*/
private String secretKey;
/**
* //"如果是true,则用的是https而不是http,默认值是true"
*/
private boolean secure;
/**
* //"默认存储桶"
*/
private String bucketName;
/**
* 图片的最大大小
*/
private long imageSize;
/**
* 其他文件的最大大小
*/
private long fileSize;
/**
* 官网给出的 构造方法,我只是去爬了一下官网 (狗头保命)
* 此类是 客户端进行操作的类
*/
@Bean
public MinioClient minioClient() {
MinioClient minioClient =
MinioClient.builder()
.credentials(accessKey, secretKey)
.endpoint(endpoint,port,secure)
.build();
return minioClient;
}
}
MinIO工具类
java
import com.example.demo.config.MinioProperties;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import io.minio.messages.Item;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Component
@RequiredArgsConstructor
public class MinioUtil {
private final MinioClient minioClient;
private final MinioProperties minioProperties;
/**
* 检查存储桶是否存在
*
* @param bucketName 存储桶名称
* @return
*/
@SneakyThrows
public boolean bucketExists(String bucketName) {
boolean found =
minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (found) {
System.out.println(bucketName + " exists");
} else {
System.out.println(bucketName + " does not exist");
}
return found;
}
/**
* 创建存储桶
*
* @param bucketName 存储桶名称
*/
@SneakyThrows
public boolean makeBucket(String bucketName) {
boolean flag = bucketExists(bucketName);
if (!flag) {
minioClient.makeBucket(
MakeBucketArgs.builder()
.bucket(bucketName)
.build());
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(RemoveBucketArgs.builder().bucket(bucketName).build());
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());
}
}else{
listObjectNames.add("存储桶不存在");
}
return listObjectNames;
}
/**
* 列出存储桶中的所有对象
*
* @param bucketName 存储桶名称
* @return
*/
@SneakyThrows
public Iterable<Result<Item>> listObjects(String bucketName) {
boolean flag = bucketExists(bucketName);
if (flag) {
return minioClient.listObjects(
ListObjectsArgs.builder().bucket(bucketName).build());
}
return null;
}
/**
* 文件上传
*
* @param bucketName
* @param multipartFile
*/
@SneakyThrows
public void putObject(String bucketName, MultipartFile multipartFile, String filename, String fileType) {
InputStream inputStream = new ByteArrayInputStream(multipartFile.getBytes());
minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(filename).stream(
inputStream, -1, minioProperties.getFileSize())
.contentType(fileType)
.build());
}
/**
* 文件访问路径
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
@SneakyThrows
public String getObjectUrl(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
String url = "";
if (flag) {
url = minioClient.getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
.method(Method.GET)
.bucket(bucketName)
.object(objectName)
.expiry(2, TimeUnit.MINUTES)
.build());
System.out.println(url);
}
return url;
}
/**
* 删除一个对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
*/
@SneakyThrows
public boolean removeObject(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
if (flag) {
minioClient.removeObject(
RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
return true;
}
return false;
}
/**
* 以流的形式获取一个文件对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
@SneakyThrows
public InputStream getObject(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
if (flag) {
StatObjectResponse statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.size() > 0) {
InputStream stream =
minioClient.getObject(
GetObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build());
return stream;
}
}
return null;
}
/**
* 获取对象的元数据
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @return
*/
@SneakyThrows
public StatObjectResponse statObject(String bucketName, String objectName) {
boolean flag = bucketExists(bucketName);
if (flag) {
StatObjectResponse stat =
minioClient.statObject(
StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
return stat;
}
return null;
}
/**
* 删除指定桶的多个文件对象,返回删除错误的对象列表,全部删除成功,返回空列表
*
* @param bucketName 存储桶名称
* @param objectNames 含有要删除的多个object名称的迭代器对象
* @return
*/
@SneakyThrows
public boolean removeObject(String bucketName, List<String> objectNames) {
boolean flag = bucketExists(bucketName);
if (flag) {
List<DeleteObject> objects = new LinkedList<>();
for (int i = 0; i < objectNames.size(); i++) {
objects.add(new DeleteObject(objectNames.get(i)));
}
Iterable<Result<DeleteError>> results =
minioClient.removeObjects(
RemoveObjectsArgs.builder().bucket(bucketName).objects(objects).build());
for (Result<DeleteError> result : results) {
DeleteError error = result.get();
System.out.println(
"Error in deleting object " + error.objectName() + "; " + error.message());
return false;
}
}
return true;
}
/**
* 以流的形式获取一个文件对象(断点下载)
*
* @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) {
StatObjectResponse statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.size() > 0) {
InputStream stream =
minioClient.getObject(
GetObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.offset(offset)
.length(length)
.build());
return stream;
}
}
return null;
}
/**
* 通过InputStream上传对象
*
* @param bucketName 存储桶名称
* @param objectName 存储桶里的对象名称
* @param inputStream 要上传的流
* @param contentType 要上传的文件类型 MimeTypeUtils.IMAGE_JPEG_VALUE
* @return
*/
@SneakyThrows
public boolean putObject(String bucketName, String objectName, InputStream inputStream,String contentType) {
boolean flag = bucketExists(bucketName);
if (flag) {
minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(
inputStream, -1, minioProperties.getFileSize())
.contentType(contentType)
.build());
StatObjectResponse statObject = statObject(bucketName, objectName);
if (statObject != null && statObject.size() > 0) {
return true;
}
}
return false;
}
}
文件类型工具类
java
import cn.hutool.core.io.FileTypeUtil;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
/**
* 文件类型工具类
*/
public class FileTypeUtils {
private final static String IMAGE_TYPE = "image/";
private final static String AUDIO_TYPE = "audio/";
private final static String VIDEO_TYPE = "video/";
private final static String APPLICATION_TYPE = "application/";
private final static String TXT_TYPE = "text/";
public static String getFileType(MultipartFile multipartFile) {
InputStream inputStream = null;
String type = null;
try {
inputStream = multipartFile.getInputStream();
type = FileTypeUtil.getType(inputStream);
System.out.println(type);
if (type.equalsIgnoreCase("JPG") || type.equalsIgnoreCase("JPEG")
|| type.equalsIgnoreCase("GIF") || type.equalsIgnoreCase("PNG")
|| type.equalsIgnoreCase("BMP") || type.equalsIgnoreCase("PCX")
|| type.equalsIgnoreCase("TGA") || type.equalsIgnoreCase("PSD")
|| type.equalsIgnoreCase("TIFF")) {
return IMAGE_TYPE+type;
}
if (type.equalsIgnoreCase("mp3") || type.equalsIgnoreCase("OGG")
|| type.equalsIgnoreCase("WAV") || type.equalsIgnoreCase("REAL")
|| type.equalsIgnoreCase("APE") || type.equalsIgnoreCase("MODULE")
|| type.equalsIgnoreCase("MIDI") || type.equalsIgnoreCase("VQF")
|| type.equalsIgnoreCase("CD")) {
return AUDIO_TYPE+type;
}
if (type.equalsIgnoreCase("mp4") || type.equalsIgnoreCase("avi")
|| type.equalsIgnoreCase("MPEG-1") || type.equalsIgnoreCase("RM")
|| type.equalsIgnoreCase("ASF") || type.equalsIgnoreCase("WMV")
|| type.equalsIgnoreCase("qlv") || type.equalsIgnoreCase("MPEG-2")
|| type.equalsIgnoreCase("MPEG4") || type.equalsIgnoreCase("mov")
|| type.equalsIgnoreCase("3gp")) {
return VIDEO_TYPE+type;
}
if (type.equalsIgnoreCase("doc") || type.equalsIgnoreCase("docx")
|| type.equalsIgnoreCase("ppt") || type.equalsIgnoreCase("pptx")
|| type.equalsIgnoreCase("xls") || type.equalsIgnoreCase("xlsx")
|| type.equalsIgnoreCase("zip")||type.equalsIgnoreCase("jar")) {
return APPLICATION_TYPE+type;
}
if (type.equalsIgnoreCase("txt")) {
return TXT_TYPE+type;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
service接口
java
import io.minio.messages.Bucket;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.List;
public interface MinioService {
/**
* 判断 bucket是否存在
*
* @param bucketName
* @return
*/
boolean bucketExists(String bucketName);
/**
* 创建 bucket
*
* @param bucketName
*/
void makeBucket(String bucketName);
/**
* 列出所有存储桶名称
* @return
*/
List<String> listBucketName();
/**
* 列出所有存储桶 信息
*
* @return
*/
List<Bucket> listBuckets();
/**
* 根据桶名删除桶
* @param bucketName
*/
boolean removeBucket(String bucketName);
/**
* 列出存储桶中的所有对象名称
* @param bucketName
* @return
*/
List<String> listObjectNames(String bucketName);
/**
* 文件上传
*
* @param multipartFile
* @param bucketName
*/
String putObject(MultipartFile multipartFile, String bucketName, String fileType);
/**
* 文件流下载
* @param bucketName
* @param objectName
* @return
*/
InputStream downloadObject(String bucketName, String objectName);
/**
* 删除文件
* @param bucketName
* @param objectName
*/
boolean removeObject(String bucketName, String objectName);
/**
* 批量删除文件
* @param bucketName
* @param objectNameList
* @return
*/
boolean removeListObject(String bucketName, List<String> objectNameList);
/**
* 获取文件路径
* @param bucketName
* @param objectName
* @return
*/
String getObjectUrl(String bucketName,String objectName);
}
impl
java
import com.example.demo.config.MinioProperties;
import com.example.demo.service.MinioService;
import com.example.demo.util.MinioUtil;
import io.minio.messages.Bucket;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;
@Service
@RequiredArgsConstructor
public class MinioServiceImpl implements MinioService {
private final MinioUtil minioUtil;
private final MinioProperties minioProperties;
@Override
public boolean bucketExists(String bucketName) {
return minioUtil.bucketExists(bucketName);
}
@Override
public void makeBucket(String bucketName) {
minioUtil.makeBucket(bucketName);
}
@Override
public List<String> listBucketName() {
return minioUtil.listBucketNames();
}
@Override
public List<Bucket> listBuckets() {
return minioUtil.listBuckets();
}
@Override
public boolean removeBucket(String bucketName) {
return minioUtil.removeBucket(bucketName);
}
@Override
public List<String> listObjectNames(String bucketName) {
return minioUtil.listObjectNames(bucketName);
}
@Override
public String putObject(MultipartFile file, String bucketName, String fileType) {
try {
// StringUtils.hasLength(bucketName) 判断传入的字串是否有内容
bucketName = StringUtils.hasLength(bucketName) ? bucketName : minioProperties.getBucketName();
if (!this.bucketExists(bucketName)) {
this.makeBucket(bucketName);
}
String fileName = file.getOriginalFilename();
String objectName = UUID.randomUUID().toString().replaceAll("-", "")
+ fileName.substring(fileName.lastIndexOf("."));
minioUtil.putObject(bucketName, file, objectName,fileType);
return minioProperties.getEndpoint()+":"+minioProperties.getPort()+"/"+bucketName+"/"+objectName;
} catch (Exception e) {
e.printStackTrace();
return "上传失败";
}
}
@Override
public InputStream downloadObject(String bucketName, String objectName) {
return minioUtil.getObject(bucketName,objectName);
}
@Override
public boolean removeObject(String bucketName, String objectName) {
return minioUtil.removeObject(bucketName, objectName);
}
@Override
public boolean removeListObject(String bucketName, List<String> objectNameList) {
return minioUtil.removeObject(bucketName,objectNameList);
}
@Override
public String getObjectUrl(String bucketName,String objectName) {
return minioUtil.getObjectUrl(bucketName, objectName);
}
}
controller
java
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.util.FileUtils;
import com.example.demo.service.MinioService;
import com.example.demo.util.FileTypeUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Objects;
@RestController
@RequestMapping("file")
@RequiredArgsConstructor
public class FileController {
// 通过构造注入业务层对象
private final MinioService minioService;
/**
* 上传
*
* @param file 文件
* @param bucketName 存储桶名称
* @return {@link String} 文件的访问路径
*/
@PostMapping("upload")
public String upload(MultipartFile file, String bucketName) {
if (Objects.isNull(file)) {
return "上传的文件不能为空!";
}
// 继续上传文件
String type = FileTypeUtils.getFileType(file);
return minioService.putObject(file, bucketName, type);
}
/**
* 列出来存储桶中的所有文件
*
* @param bucketName bucket名称
* @return {@link String} 所有文件
*/
@GetMapping("list")
public List<String> list(String bucketName) {
return minioService.listObjectNames(bucketName);
}
}