来源:blog.csdn.net/HXNLYW/article/details/109744670
MinIo简介
MinIO 是一个基于Apache License v2.0开源协议的对象存储服务。它兼容亚马逊S3云存储服务接口,非常适合于存储大容量非结构化的数据,例如图片、视频、日志文件、备份数据和容器/虚拟机镜像等,而一个对象文件可以是任意大小,从几kb到最大5T不等。
MinIO官方文档:
https://docs.min.io/cn/ https://docs.min.io/docs/java-client-api-reference.html
安装
docker 安装:
docker run -p 9000:9000 --name minio1 \
-e "MINIO_ACCESS_KEY=gourd.hu" \
-e "MINIO_SECRET_KEY=gourd123456" \
-v /mnt/data:/data \
-v /mnt/config:/root/.minio \
minio/minio server /data复制
docker-compose 安装:
version: '3.3'
services:
# minio
minio:
image: minio/minio
container_name: minio
hostname: minio
command: server /data
restart: always
ports:
- "9000:9000"
volumes:
- /home/gourd/minio/data:/data
- /home/gourd/minio/config:/root/.minio
environment:
# 配置域名或者公网IP端口。
- MINIO_DOMAIN=http://111.231.111.150:9000
- MINIO_ACCESS_KEY=gourd.hu
- MINIO_SECRET_KEY=gourd123456复制
注意:MINIO_SECRET_KEY密钥必须大于8位,否则会创建失败
管理后台
安装完之后,可登录管理后台查看文件、修改密码等。
地址:你配置的MINIO_DOMAIN 账号:默认你配置的MINIO_ACCESS_KEY 密码:默认你配置的MINIO_SECRET_KEY
springboot整合
pom.xml中引入MinIO依赖
<!--minio-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.0.1</version>
</dependency>复制
application.yml中配置MinIO
# MinIo文件服务器
min:
io:
endpoint: http://111.231.111.150:9000
accessKey: gourd.hu
secretKey: gourd123456复制
核心类 minio配置属性类
/**
* minio配置属性
*
* @author gourd.hu
*/
@Data
@ConfigurationProperties(prefix = "min.io")
public class MinIoProperties {
/**
* Minio 服务地址
*/
private String endpoint;
/**
* Minio ACCESS_KEY
*/
private String accessKey;
/**
* Minio SECRET_KEY
*/
private String secretKey;
}复制
minio工具类,列出了常用的文件操作,如需其他的操作,需要参看官网java-api文档:https://docs.min.io/cn/java-client-api-reference.html
/**
* minio工具类
*
* @author gourd.hu
*/
@Configuration
@EnableConfigurationProperties({MinIoProperties.class})
public class MinIoUtil {
private MinIoProperties minIo;
public MinIoUtil(MinIoProperties minIo) {
this.minIo = minIo;
}
private static MinioClient minioClient;
@PostConstruct
public void init() {
try {
minioClient = MinioClient.builder()
.endpoint(minIo.getEndpoint())
.credentials(minIo.getAccessKey(), minIo.getSecretKey())
.build();
} catch (Exception e) {
ResponseEnum.MIN_IO_INIT_FAIL.assertFail(e);
}
}
/**
* 判断 bucket是否存在
*
* @param bucketName
* @return
*/
public static boolean bucketExists(String bucketName) {
try {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
ResponseEnum.MIN_IO_BUCKET_CHECK_FAIL.assertFail(e);
}
return false;
}
/**
* 判断 bucket是否存在
*
* @param bucketName
* @param region
* @return
*/
public static boolean bucketExists(String bucketName, String region) {
try {
return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).region(region).build());
} catch (Exception e) {
ResponseEnum.MIN_IO_BUCKET_CHECK_FAIL.assertFail(e);
}
return false;
}
/**
* 创建 bucket
*
* @param bucketName
*/
public static void makeBucket(String bucketName) {
try {
boolean isExist = bucketExists(bucketName);
if (!isExist) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
}
} catch (Exception e) {
ResponseEnum.MIN_IO_BUCKET_CREATE_FAIL.assertFail(e);
}
}
/**
* 创建 bucket
*
* @param bucketName
* @param region
*/
public static void makeBucket(String bucketName, String region) {
try {
boolean isExist = bucketExists(bucketName, region);
if (!isExist) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).region(region).build());
}
} catch (Exception e) {
ResponseEnum.MIN_IO_BUCKET_CREATE_FAIL.assertFail(e);
}
}
/**
* 删除 bucket
*
* @param bucketName
* @return
*/
public static void deleteBucket(String bucketName) {
try {
minioClient.deleteBucketEncryption(
DeleteBucketEncryptionArgs.builder().bucket(bucketName).build());
} catch (Exception e) {
ResponseEnum.MIN_IO_BUCKET_DELETE_FAIL.assertFail(e);
}
}
/**
* 删除 bucket
*
* @param bucketName
* @return
*/
public static void deleteBucket(String bucketName, String region) {
try {
minioClient.deleteBucketEncryption(
DeleteBucketEncryptionArgs.builder().bucket(bucketName).bucket(region).build());
} catch (Exception e) {
ResponseEnum.MIN_IO_BUCKET_DELETE_FAIL.assertFail(e);
}
}
/**
* 文件上传
*
* @param bucketName
* @param objectName
* @param filePath
*/
public static void uploadObject(String bucketName, String objectName, String filePath) {
putObject(bucketName, null, objectName, filePath);
}
/**
* 文件上传
*
* @param bucketName
* @param objectName
* @param filePath
*/
public static void uploadObject(String bucketName, String region, String objectName, String filePath) {
putObject(bucketName, region, objectName, filePath);
}
/**
* 文件上传
*
* @param multipartFile
* @param bucketName
* @return
*/
public static String uploadObject(MultipartFile multipartFile, String bucketName) {
// bucket 不存在,创建
if (!bucketExists(bucketName)) {
makeBucket(bucketName);
}
return putObject(multipartFile, bucketName, null);
}
/**
* 文件上传
*
* @param multipartFile
* @param bucketName
* @return
*/
public static String uploadObject(MultipartFile multipartFile, String bucketName, String region) {
// bucket 不存在,创建
if (!bucketExists(bucketName, region)) {
makeBucket(bucketName, region);
}
return putObject(multipartFile, bucketName, region);
}
/**
* 文件下载
*
* @param bucketName
* @param region
* @param fileName
* @return
*/
public static void downloadObject(String bucketName, String region, String fileName) {
HttpServletResponse response = RequestHolder.getResponse();
// 设置编码
response.setCharacterEncoding(XmpWriter.UTF8);
try (ServletOutputStream os = response.getOutputStream();
GetObjectResponse is = minioClient.getObject(
GetObjectArgs.builder()
.bucket(bucketName)
.region(region)
.object(fileName)
.build());) {
response.setHeader("Content-Disposition", "attachment;fileName=" +
new String(fileName.getBytes("gb2312"), "ISO8859-1"));
ByteStreams.copy(is, os);
os.flush();
} catch (Exception e) {
ResponseEnum.MIN_IO_FILE_DOWNLOAD_FAIL.assertFail(e);
}
}
/**
* 获取文件
*
* @param bucketName
* @param region
* @param fileName
* @return
*/
public static String getObjectUrl(String bucketName, String region, String fileName) {
String objectUrl = null;
try {
objectUrl = minioClient.getPresignedObjectUrl(
GetPresignedObjectUrlArgs.builder()
.method(Method.GET)
.bucket(bucketName)
.region(region)
.object(fileName)
.build());
} catch (Exception e) {
ResponseEnum.MIN_IO_FILE_GET_FAIL.assertFail(e);
}
return objectUrl;
}
/**
* 删除文件
*
* @param bucketName
* @param objectName
*/
public static void deleteObject(String bucketName, String objectName) {
try {
minioClient.removeObject(
RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
} catch (Exception e) {
ResponseEnum.MIN_IO_FILE_DELETE_FAIL.assertFail(e);
}
}
/**
* 删除文件
*
* @param bucketName
* @param region
* @param objectName
*/
public static void deleteObject(String bucketName, String region, String objectName) {
try {
minioClient.removeObject(
RemoveObjectArgs.builder().bucket(bucketName).region(region).object(objectName).build());
} catch (Exception e) {
ResponseEnum.MIN_IO_FILE_DELETE_FAIL.assertFail(e);
}
}
/**
* 上传文件
*
* @param multipartFile
* @param bucketName
* @return
*/
private static String putObject(MultipartFile multipartFile, String bucketName, String region) {
try (InputStream inputStream = multipartFile.getInputStream()) {
// 上传文件的名称
String fileName = multipartFile.getOriginalFilename();
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucketName)
.region(region)
.object(fileName)
.stream(inputStream, multipartFile.getSize(), ObjectWriteArgs.MIN_MULTIPART_SIZE)
.contentType(multipartFile.getContentType())
.build());
// 返回访问路径
return getObjectUrl(bucketName, region, fileName);
} catch (Exception e) {
ResponseEnum.MIN_IO_FILE_UPLOAD_FAIL.assertFail(e);
}
return null;
}
/**
* 上传文件
*
* @param bucketName
* @param region
* @param objectName
* @param filePath
*/
private static String putObject(String bucketName, String region, String objectName, String filePath) {
try {
minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket(bucketName)
.region(region)
.object(objectName)
.filename(filePath)
.build());
// 返回访问路径
return getObjectUrl(bucketName, region, objectName);
} catch (Exception e) {
ResponseEnum.MIN_IO_FILE_UPLOAD_FAIL.assertFail(e);
}
return null;
}
}复制
测试
controller控制器入口方法,上传完之后可以在管理后台查看到。
/**
* minio上传文件
*
*/
@PostMapping("/minio-upload")
@ApiOperation(value="minio上传文件")
public BaseResponse minioUpload(MultipartFile file){
return BaseResponse.ok(MinIoUtil.uploadObject(file,"gourd","suzhou"));
}
/**
* minio下载文件
*
*/
@GetMapping("/minio-download")
@ApiOperation(value="minio下载文件")
public void minioDownload(){
MinIoUtil.downloadObject("gourd","suzhou","paixu.gif");
}复制
更多案例
cnblogs.com/stonechen/p/14298748.html
文章转载自爱编码,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
2025年4月中国数据库流行度排行榜:OB高分复登顶,崖山稳驭撼十强
墨天轮编辑部
2502次阅读
2025-04-09 15:33:27
数据库国产化替代深化:DBA的机遇与挑战
代晓磊
1161次阅读
2025-04-27 16:53:22
2025年3月国产数据库中标情况一览:TDSQL大单622万、GaussDB大单581万……
通讯员
844次阅读
2025-04-10 15:35:48
2025年4月国产数据库中标情况一览:4个千万元级项目,GaussDB与OceanBase大放异彩!
通讯员
659次阅读
2025-04-30 15:24:06
数据库,没有关税却有壁垒
多明戈教你玩狼人杀
576次阅读
2025-04-11 09:38:42
天津市政府数据库框采结果公布,7家数据库产品入选!
通讯员
561次阅读
2025-04-10 12:32:35
国产数据库需要扩大场景覆盖面才能在竞争中更有优势
白鳝的洞穴
537次阅读
2025-04-14 09:40:20
【活动】分享你的压箱底干货文档,三篇解锁进阶奖励!
墨天轮编辑部
475次阅读
2025-04-17 17:02:24
一页概览:Oracle GoldenGate
甲骨文云技术
456次阅读
2025-04-30 12:17:56
GoldenDB数据库v7.2焕新发布,助力全行业数据库平滑替代
GoldenDB分布式数据库
451次阅读
2025-04-30 12:17:50