一、minio
是一个高性能的对象存储原生支持
部署的解决方案。
提供了一个
兼容
并支持所有核心
功能。
对象存储使用
来组织对象。 存储桶类似于文件系统中的文件夹或目录,其中每个 桶可以容纳任意数量的对象。
存储桶提供 与
存储桶相同的功能。
其中
的优势有:
高性能:
是全球领先的对象存储先锋,在标准硬件上,读/写速度上高达
和
。
可扩展性:
利用了
缩放器的来之不易的知识,为对象存储带来了简单的存储缩放模型, 在
, 扩展从单个群集开始,该群集可以与其他
群集联合以创建全局名称空间, 并在需要时可以跨越多个不同的数据中心。 通过添加更多集群可以扩展名称空间, 更多机架,直到实现目标。
云原生支持:
是在过去4年的时间内从0开始打造的一款软件 ,符合一切原生云计算的架构和构建过程,并且包含最新的云计算的全新的技术和概念。 其中包括支持
、微服和多租户的的容器技术。使对象存储对于
更加友好。
源码开放与企业级支持:
基于
开放源代码 。 这就意味着
的客户能够自动的、无限制、自由免费使用和集成
、自由的创新和创造、 自由的去修改、自由的再次发行新的版本和软件. 确实,
强有力的支持和驱动了很多世界500强的企业。 此外,其部署的多样性和专业性提供了其他软件无法比拟的优势。
在实验开始前请确保安装完成了
:
二、SpringBoot 使用 Minio 进行文件存储
首先新建一个
项目,在
中引入
依赖:
在配置文件中,声明出
的信息:
下面创建一个配置类,对
进行创建:
下面创建一个工具类
将常用的操作封装在工具类中:
编写测试接口,进行测试:
三、测试
测试上传文件:
如果使用 返回的 url 直接访问文件,可以发现返回权限不足:
这里需要改一下
的
,默认为
,可以修改为
便无需认证,但安全性无法保证:
再次进行访问,文件就可以打开了:
如果需要保持
,可以通过
进行下载,使用
测试接口下载文件:
miniokubernetesminioamazon web services s3apis3MinIObucketsMinIOAWS S3MinIOMinIO183 GB / 秒171 GB / 秒MinIOwebMinIOMinIOMinIOKubernetesKubernetesMinIOApache V2 license 100%MinIOMinIOMinIOminioSpringBootpomminio
io.minio
minio
8.2.1
miniominio:
url: http://192.168.40.169:9000 # minio配置的地址,端口9000,注意不是控制台的端口
accessKey: minioadmin # 账号
secretKey: minioadmin # 密码
bucketName: test-bucket # MinIO桶名字MinioClient@Data
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
/**
* 服务地址
*/
private String url;
/**
* 用户名
*/
private String accessKey;
/**
* 密码
*/
private String secretKey;
/**
* 存储桶名称
*/
private String bucketName;
@Bean
public MinioClient getMinioClient() throws Exception {
MinioClient minioClient = MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).build();
//判断桶是否存在,不存在则新建
if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())){
minioClient.makeBucket(MakeBucketArgs.builder()
.bucket(bucketName)
.build());
}
return minioClient;
}
}MinioTool@Component
public class MinioTool {
@Autowired
private MinioClient minioClient;
@Autowired
private MinioConfig minioConfig;
/**
* 查看存储bucket是否存在
*
* @param bucketName 存储bucket
* @return boolean
*/
public Boolean bucketExists(String bucketName) {
Boolean found;
try {
found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return found;
}
/**
* 创建存储bucket
*
* @param bucketName 存储bucket名称
* @return Boolean
*/
public Boolean makeBucket(String bucketName) {
try {
minioClient.makeBucket(MakeBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 删除存储bucket
*
* @param bucketName 存储bucket名称
* @return Boolean
*/
public Boolean removeBucket(String bucketName) {
try {
minioClient.removeBucket(RemoveBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 查看文件对象
*
* @param bucketName 存储bucket名称
* @return 存储bucket内文件对象信息
*/
public Iterable> listObjects(String bucketName) {
Iterable> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket(bucketName).build());
return results;
}
/**
* 批量删除文件对象
*
* @param bucketName 存储bucket名称
* @param objects 对象名称集合
*/
public Iterable> removeObjects(String bucketName, List objects) {
List dos = objects.stream().map(e -> new DeleteObject(e)).collect(Collectors.toList());
Iterable> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());
return results;
}
/**
* 文件上传
* 文件名称相同会覆盖
* @param file 文件
* @return Boolean
*/
public Boolean upload(MultipartFile file, String fileName) {
try {
if (!bucketExists(minioConfig.getBucketName())) {
makeBucket(minioConfig.getBucketName());
}
PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(minioConfig.getBucketName()).object(fileName)
.stream(file.getInputStream(), file.getSize(), -1).contentType(file.getContentType()).build();
minioClient.putObject(objectArgs);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 文件下载
*
* @param fileName 文件名称
* @param res response
* @return Boolean
*/
public void download(String fileName, HttpServletResponse res) {
GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(minioConfig.getBucketName())
.object(fileName).build();
try (GetObjectResponse response = minioClient.getObject(objectArgs)) {
byte[] buf = new byte[1024];
int len;
try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()) {
while ((len = response.read(buf)) != -1) {
os.write(buf, 0, len);
}
os.flush();
byte[] bytes = os.toByteArray();
res.setCharacterEncoding("utf-8");
//设置强制下载不打开
res.setContentType("application/force-download");
res.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
try (ServletOutputStream stream = res.getOutputStream()) {
stream.write(bytes);
stream.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String getFileUrl(String fileName){
return StringFormatter.concat(minioConfig.getUrl(), "/", minioConfig.getBucketName(), "/", fileName).getValue();
}
} @Component
public class MinioTool {
@Autowired
private MinioClient minioClient;
@Autowired
private MinioConfig minioConfig;
/**
* 查看存储bucket是否存在
*
* @param bucketName 存储bucket
* @return boolean
*/
public Boolean bucketExists(String bucketName) {
Boolean found;
try {
found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return found;
}
/**
* 创建存储bucket
*
* @param bucketName 存储bucket名称
* @return Boolean
*/
public Boolean makeBucket(String bucketName) {
try {
minioClient.makeBucket(MakeBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 删除存储bucket
*
* @param bucketName 存储bucket名称
* @return Boolean
*/
public Boolean removeBucket(String bucketName) {
try {
minioClient.removeBucket(RemoveBucketArgs.builder()
.bucket(bucketName)
.build());
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 查看文件对象
*
* @param bucketName 存储bucket名称
* @return 存储bucket内文件对象信息
*/
public Iterable> listObjects(String bucketName) {
Iterable> results = minioClient.listObjects(
ListObjectsArgs.builder().bucket(bucketName).build());
return results;
}
/**
* 批量删除文件对象
*
* @param bucketName 存储bucket名称
* @param objects 对象名称集合
*/
public Iterable> removeObjects(String bucketName, List objects) {
List dos = objects.stream().map(e -> new DeleteObject(e)).collect(Collectors.toList());
Iterable> results = minioClient.removeObjects(RemoveObjectsArgs.builder().bucket(bucketName).objects(dos).build());
return results;
}
/**
* 文件上传
* 文件名称相同会覆盖
* @param file 文件
* @return Boolean
*/
public Boolean upload(MultipartFile file, String fileName) {
try {
if (!bucketExists(minioConfig.getBucketName())) {
makeBucket(minioConfig.getBucketName());
}
PutObjectArgs objectArgs = PutObjectArgs.builder().bucket(minioConfig.getBucketName()).object(fileName)
.stream(file.getInputStream(), file.getSize(), -1).contentType(file.getContentType()).build();
minioClient.putObject(objectArgs);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 文件下载
*
* @param fileName 文件名称
* @param res response
* @return Boolean
*/
public void download(String fileName, HttpServletResponse res) {
GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(minioConfig.getBucketName())
.object(fileName).build();
try (GetObjectResponse response = minioClient.getObject(objectArgs)) {
byte[] buf = new byte[1024];
int len;
try (FastByteArrayOutputStream os = new FastByteArrayOutputStream()) {
while ((len = response.read(buf)) != -1) {
os.write(buf, 0, len);
}
os.flush();
byte[] bytes = os.toByteArray();
res.setCharacterEncoding("utf-8");
//设置强制下载不打开
res.setContentType("application/force-download");
res.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
try (ServletOutputStream stream = res.getOutputStream()) {
stream.write(bytes);
stream.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String getFileUrl(String fileName){
return StringFormatter.concat(minioConfig.getUrl(), "/", minioConfig.getBucketName(), "/", fileName).getValue();
}
} BucketAccess PolicyprivatepublicprivateMinioClientdownloadhttp://localhost:8080/file/download/20cab4e3979eba6003f95aca0dc75c63.jpg