目录
MinIO概念
- MinIO 是在 GNU Affero 通用公共许可证 v3.0 下发布的高性能对象存储。它与 Amazon S3 云存储服务 API 兼容。使用 MinIO 为机器学习、分析和应用程序数据工作负载构建高性能基础架构。
- MinIO是高性能对象存储,什么是对象存储(Object Storage Service),对象存储是支持海量用户远程访问的无限容量廉价存储系统,即是存储系统。
- 它由桶(bucket,对应Windows下的文件夹),组成目录结构,桶中直接存放对象(Object,对应Windwos下的文件),桶中不能再创建桶,但是能创建文件夹。
MinIO下载安装启动
访问官网:https://docs.min.io/
进入官网点击右上角Download
下载
点击Windows下载服务端
安装
将MinIO.exe 拷贝到自己的目录下去,并创建一个data目录
启动
通过命令行启动Minio :
minio server ./data
启动完成后,可以看到访问的路径:http://127.0.0.1:9000
使用提供的账号密码进行登录
账号:minioadmin
密码:minioadmin
MinIO实现上传文件
导入maven坐标
<!--minio -->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>8.4.3</version>
</dependency>
在application文件添加配置
# MinIO配置
minio:
endpoint: 127.0.0.1
port: 9000
accessKey: minioadmin
secretKey: minioadmin
secure: false
bucketName: "demo"
configDir: "/data/excel"
实现代码
/**
* 读取配置文件的内容赋值给属性
*/
@Data
@Component
@ConfigurationProperties(prefix = "minio")
public class MinioConfig {
private final static String HTTP = "http://";
//endPoint是一个URL,域名,IPv4或者IPv6地址
private String endpoint;
//TCP/IP端口号
private int port;
//accessKey类似于用户ID,用于唯一标识你的账户
private String accessKey;
//secretKey是你账户的密码
private String secretKey;
//如果是true,则用的是https而不是http,默认值是true
private Boolean secure;
//默认存储桶
private String bucketName;
}
/**
* 返回结果封装
*/
public class AjaxResult extends HashMap<String, Object>
{
private static final long serialVersionUID = 1L;
/** 状态码 */
public static final String CODE_TAG = "code";
/** 返回内容 */
public static final String MSG_TAG = "msg";
/** 数据对象 */
public static final String DATA_TAG = "data";
/**
* 初始化一个新创建的 AjaxResult 对象,使其表示一个空消息。
*/
public AjaxResult()
{
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
*/
public AjaxResult(int code, String msg)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
}
/**
* 初始化一个新创建的 AjaxResult 对象
*
* @param code 状态码
* @param msg 返回内容
* @param data 数据对象
*/
public AjaxResult(int code, String msg, Object data)
{
super.put(CODE_TAG, code);
super.put(MSG_TAG, msg);
if (StringUtils.isNotNull(data))
{
super.put(DATA_TAG, data);
}
}
/**
* 返回成功消息
*
* @return 成功消息
*/
public static AjaxResult success()
{
return AjaxResult.success("操作成功");
}
/**
* 返回成功数据
*
* @return 成功消息
*/
public static AjaxResult success(Object data)
{
return AjaxResult.success("操作成功", data);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @return 成功消息
*/
public static AjaxResult success(String msg)
{
return AjaxResult.success(msg, null);
}
/**
* 返回成功消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 成功消息
*/
public static AjaxResult success(String msg, Object data)
{
return new AjaxResult(HttpStatus.SUCCESS, msg, data);
}
/**
* 返回错误消息
*
* @return
*/
public static AjaxResult error()
{
return AjaxResult.error("操作失败");
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @return 警告消息
*/
public static AjaxResult error(String msg)
{
return AjaxResult.error(msg, null);
}
/**
* 返回错误消息
*
* @param msg 返回内容
* @param data 数据对象
* @return 警告消息
*/
public static AjaxResult error(String msg, Object data)
{
return new AjaxResult(HttpStatus.ERROR, msg, data);
}
/**
* 返回错误消息
*
* @param code 状态码
* @param msg 返回内容
* @return 警告消息
*/
public static AjaxResult error(int code, String msg)
{
return new AjaxResult(code, msg, null);
}
}
/**
* 通用请求处理
*/
@RestController
public class CommonController extends BaseController {
private static final Logger log = LoggerFactory.getLogger(CommonController.class);
@Autowired
private ISysFileService fileService;
/**
* 通用上传请求
*/
@PostMapping("/common/upload")
public AjaxResult uploadFile(MultipartFile file){
try{
// 上传文件路径
return fileService.upload(file);
}catch (Exception e){
return AjaxResult.error(e.getMessage());
}
}
}
public interface ISysFileService {
/**
* 文件上传
* @param file
* @return
*/
AjaxResult upload(MultipartFile file);
}
@Service
@Slf4j
public class SysFileServiceImpl implements ISysFileService{
@Autowired
MinioConfig minioConfig;
/**
* 文件上传至Minio
* 使用try catch finally进行上传
* finally里进行资源的回收
*/
@Override
public AjaxResult upload(MultipartFile file) {
InputStream inputStream = null;
//创建Minio的连接对象
MinioClient minioClient = getClient();
//获取存储桶
String bucketName = minioConfig.getBucketName();
try {
inputStream = file.getInputStream();
// Make bucketName bucket if not exist.
boolean found =
minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
if (!found) {
// Make a new bucket called bucketName.
minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
} else {
log.info("Bucket " +"["+bucketName+ "]"+" already exists.");
}
//Uploads given stream as object in bucket.
String oldFileName = file.getOriginalFilename();
String objectName = new SimpleDateFormat("yyyy/MM/dd/").format(new Date())+IdUtils.simpleUUID()+oldFileName.substring(oldFileName.lastIndexOf("."));
minioClient.putObject(
PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(
inputStream, file.getSize(), -1)
.contentType(file.getContentType())
.build());
/**
* 构建返回结果集
*/
AjaxResult ajax = AjaxResult.success();
/**
* 封装需要的数据进行返回
*/
ajax.put("fileName", "/"+bucketName+"/"+objectName);
ajax.put("url", "http://"+minioConfig.getEndpoint()+":"+minioConfig.getPort()+"/"+minioConfig.getBucketName()+"/"+objectName);
return ajax;
}catch(Exception e){
e.printStackTrace();
return AjaxResult.error("上传失败");
}finally {
//防止内存泄漏
if (inputStream != null) {
try {
inputStream.close(); // 关闭流
} catch (IOException e) {
log.debug("inputStream close IOException:" + e.getMessage());
}
}
}
}
/**
* 提供一个获取Minio连接的方法
* 获取Minio连接
* @return
*/
private MinioClient getClient(){
MinioClient minioClient =
MinioClient.builder()
.endpoint("http://"+minioConfig.getEndpoint()+":"+ minioConfig.getPort())
.credentials(minioConfig.getAccessKey(),minioConfig.getSecretKey())
.build();
return minioClient;
}
}
响应数据格式
{
"msg":"操作成功",
"fileName":"/demo/2022/10/14/efcb6266efba42a4b35344181964be36.pdf",
"code":200,
"url":"http://127.0.0.1:9000/demo/2022/10/14/efcb6266efba42a4b35344181964be36.pdf"
}
到此,上传文件到MinIO服务器就完成了。