⼿把⼿教你SpringBoot轻松整合Minio
⽬录
前⾔
⼀、技术介绍
1.Minio是什么?
⼆、使⽤步骤
1.引⼊maven库
2.封装Minio
3.配置⽂件
4.单元测试
总结
前⾔
使⽤Spring Boot 可以⾮常⽅便、快速搭建项⽬,使我们不⽤关⼼框架之间的兼容性,适⽤版本等各种问题,我们想使⽤任何东西,仅仅添加⼀个配置就可以。
提⽰:以下是本篇⽂章正⽂内容,下⾯案例可供参考
⼀、技术介绍
1.Minio是什么?
MinIO 是⼀个基于Apache License v2.0开源协议的对象存储服务。它兼容亚马逊S3云存储服务接⼝,⾮常适合于存储⼤容量⾮结构化的数据,例如图⽚、视频、⽇志⽂件、备份数据和容器/虚拟机镜像等,⽽⼀个对象⽂件可以是任意⼤⼩,从⼏kb到最⼤5T不等。
⼆、使⽤步骤
1.引⼊maven库
代码如下(⽰例):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.0.3</version>
</dependency>
</dependencies>
2.封装Minio
IFile类:
package com.hyh.minio;
/**
* File接⼝类
*
* @Author: heyuhua
* @Date: 2021/1/12 10:33
*/
public interface IFile {
/**
* 上传
*
* @param filename ⽂件名
*/
void upload(String filename);
/**
* 上传
*
* @param filename ⽂件名
* @param object 保存对象⽂件名称
*/
void upload(String filename, String object);
}
File接⼝类:
package com.hyh.minio.service;
import com.hyh.minio.IFile;
/**
* File接⼝
*
* @Author: heyuhua
* @Date: 2021/1/12 10:53
*/
public interface FileService extends IFile {
/**
* 上传
*
* @param filename ⽂件名称
* @param object 保存对象⽂件名称
* @param bucket 存储桶
*/
void upload(String filename, String object, String bucket);
}
File接⼝实现类:
package com.hyh.minio.service.impl;
import com.hyh.minio.service.FileService;
import com.hyh.utilsmon.StringUtils;
import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import s.MinioException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
* ⽂件接⼝服务实现
*
* @Author: heyuhua
* @Date: 2021/1/12 10:53
*/
@Service
public class FileServiceImpl implements FileService {
/**
* ⽇志
*/
private static final Logger LOG = Logger(FileServiceImpl.class);
/**
* minio 客户端
*/
@Autowired
private MinioClient minioClient;
/
**
* 默认存储桶名称
*/
@Value("${minio.bucketName:}")
private String defaultBucketName;
@Override
public void upload(String filename) {
uploadObject(filename, null, defaultBucketName);
}
@Override
public void upload(String filename, String object) {
uploadObject(filename, object, defaultBucketName);
}
@Override
public void upload(String filename, String object, String bucket) {
uploadObject(filename, object, bucket);
}
/**
* 上传
*
* @param filename
* @param object
* @param bucket
*/
private void uploadObject(String filename, String object, String bucket) {
if (StringUtils.isAnyBlank(filename, bucket))
return;
try {
//存储桶构建
bucketBuild(bucket);
//保存的⽂件名称
object = StringUtils.isBlank(object) ? filename.substring(filename.lastIndexOf("/") > 0 ? filename.lastIndexOf("/") : filename.lastIndexOf("\\")) : object; minioClient.uploadObject(
UploadObjectArgs.builder()
.bucket(bucket)
.object(object)
.filename(filename)
.build());
} catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException exception) {
<("uploadObject error", exception);
}
}
/**
* 存储桶构建
*
* @param bucketName
*/
private void bucketBuild(String bucketName) {
try {
boolean found =
minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (!found) {
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
LOG.info("Bucket " + bucketName + " make success.");
} else {
LOG.info("Bucket " + bucketName + " already exists.");
}
} catch (MinioException | InvalidKeyException | IOException | NoSuchAlgorithmException exception) { ("bucketBuild error", exception);
}
}
public String getDefaultBucketName() {
return defaultBucketName;
}
public void setDefaultBucketName(String defaultBucketName) {
this.defaultBucketName = defaultBucketName;
}
}
Minio配置类:
package com.fig;
import io.minio.MinioClient;
import org.t.properties.ConfigurationProperties;
import t.annotation.Bean;
import t.annotation.Configuration;
/**
* @Author: heyuhua
* @Date: 2021/1/12 10:42
*/
@Configuration
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
/**
* endPoint是⼀个URL,域名,IPv4或者IPv6地址
*/
private String endpoint;
/**
* 端⼝
*/
private int port;
/**
* accessKey类似于⽤户ID,⽤于唯⼀标识你的账户
*/
private String accessKey;
/**
* secretKey是你账户的密码
*/
private String secretKey;
/**
* 如果是true,则⽤的是https⽽不是http,默认值是true
*/
private Boolean secure;
/
**
* 默认存储桶
*/
private String bucketName;
/**
* 配置⽬录
*/
private String configDir;
@Bean
public MinioClient getMinClient() {
return MinioClient.builder()
.
endpoint(endpoint, port, secure)//http
.credentials(accessKey, secretKey)
.build();
}
public String getEndpoint() {
spring framework是什么框架的return endpoint;
}
public void setEndpoint(String endpoint) {
}
public String getAccessKey() {
return accessKey;
}
public void setAccessKey(String accessKey) { this.accessKey = accessKey;
}
public String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
this.secretKey = secretKey;
}
public Boolean getSecure() {
return secure;
}
public void setSecure(Boolean secure) {
this.secure = secure;
}
public String getBucketName() {
return bucketName;
}
public void setBucketName(String bucketName) { this.bucketName = bucketName;
}
public String getConfigDir() {
return configDir;
}
public void setConfigDir(String configDir) {
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
Minio助⼿类封装:
package com.hyh.minio.helper;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论