Minio文件分片上传实现

资源准备
MacM1Pro 安装Parallels19.1.0请参考 https://blog.csdn.net/qq_41594280/article/details/135420241
MacM1Pro Parallels安装CentOS7.9请参考 https://blog.csdn.net/qq_41594280/article/details/135420461
部署Minio和整合SpringBoot请参考 https://blog.csdn.net/qq_41594280/article/details/135613722

Minio Paralles虚拟机文件百度网盘获取地址: MinioParallelsVMFile
代码(含前后端)可参考 minio-chunk-upload-demo

# 1.ide拉取代码启动(AppMain)后端服务
# 2.cd vue-minio-upload-sample
# 3.npm install
# 4.npm run dev
# 5.访问 http:127.0.0.1:8080 进行测试

一、准备表结构

1.1 文件上传信息表

CREATE TABLE minio_file_upload_info(`id` BIGINT(20) PRIMARY KEY AUTO_INCREMENT NOT NULL COMMENT '自增主键',`file_name` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '文件名称',`file_md5` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '文件MD5',`upload_id` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '文件上传Id',`file_url` VARCHAR(255) NOT NULL DEFAULT '' COMMENT '文件路径',`total_chunk` INT(10) NOT NULL DEFAULT 0 COMMENT '文件总分块数',`file_status` VARCHAR(50) NOT NULL DEFAULT '' COMMENT '文件状态',`update_time` DATETIME DEFAULT NULL COMMENT '修改时间') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='文件上传信息';

1.2 分块上传信息表

CREATE TABLE minio_chunk_upload_info(`id` BIGINT(20) PRIMARY KEY AUTO_INCREMENT NOT NULL COMMENT '自增主键',`chunk_number` INT(10) NOT NULL DEFAULT 0 COMMENT '文件分片号',`file_md5` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '文件MD5',`upload_id` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '文件上传Id',`chunk_upload_url` VARCHAR(1000) NOT NULL DEFAULT '' COMMENT '文件分片路径',`expiry_time` DATETIME DEFAULT NULL COMMENT '失效时间') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='文件分片信息';

二、Minio文件相关基操

2.1 Entity

/*** 文件上传信息*/
@TableName(schema = "minio_demo", value = "minio_file_upload_info")
@Data
public class MinioFileUploadInfo {/*** 自增ID*/@TableId(type = IdType.AUTO)private Long id;/*** 文件名称*/private String fileName;/*** 文件Md5值*/private String fileMd5;/*** 文件上传ID*/private String uploadId;/*** 文件路径*/private String fileUrl;/*** 总分块数*/private Integer totalChunk;/*** 文件上传状态*/private String fileStatus;/*** 修改时间*/private Date updateTime;}
/*** 文件分片信息*/
@TableName(schema = "minio_demo", value = "minio_chunk_upload_info")
@Data
public class MinioFileChunkUploadInfo implements Serializable {/*** 自增ID*/@TableId(type = IdType.AUTO)private Long id;/*** 文件Md5值*/private String fileMd5;/*** 上传ID*/private String uploadId;/*** 文件块号*/private Integer chunkNumber;/*** 文件块上传URL*/private String chunkUploadUrl;/*** 过期时间*/private LocalDateTime expiryTime;}

2.2 Mapper

public interface MinioFileUploadInfoMapper extends MyBaseMapper<MinioFileUploadInfo> {
}
public interface MinioFileChunkUploadInfoMapper extends MyBaseMapper<MinioFileChunkUploadInfo> {
}

2.3 Service

public interface MinioFileUploadInfoService extends IService<MinioFileUploadInfo> {/*** 根据文件 md5 查询** @param fileMd5 文件 md5*/MinioFileUploadInfoDTO getByFileMd5(String fileMd5);/*** 保存** @param param 参数对象*/MinioFileUploadInfoDTO saveMinioFileUploadInfo(MinioFileUploadInfoParam param);/*** 修改文件状态** @param param 参数对象*/int updateFileStatusByFileMd5(MinioFileUploadInfoParam param);}
@Service
public class MinioFileUploadInfoServiceImplextends ServiceImpl<MinioFileUploadInfoMapper, MinioFileUploadInfo>implements MinioFileUploadInfoService {@Overridepublic MinioFileUploadInfoDTO getByFileMd5(String fileMd5) {MinioFileUploadInfo minioFileUploadInfo = this.baseMapper.selectOne(new LambdaQueryWrapper<MinioFileUploadInfo>().eq(MinioFileUploadInfo::getFileMd5, fileMd5));if (null == minioFileUploadInfo) {return null;}return ExtBeanUtils.doToDto(minioFileUploadInfo, MinioFileUploadInfoDTO.class);}@Overridepublic MinioFileUploadInfoDTO saveMinioFileUploadInfo(MinioFileUploadInfoParam param) {MinioFileUploadInfo minioFileUploadInfo;if (null == param.getId()) {minioFileUploadInfo = new MinioFileUploadInfo();} else {minioFileUploadInfo = this.baseMapper.selectById(param.getId());if (null == minioFileUploadInfo) {throw new MinioDemoException(MinioDemoExceptionTypes.DATA_NOT_EXISTED);}minioFileUploadInfo.setUpdateTime(new Date());}BeanUtils.copyProperties(param, minioFileUploadInfo, "id");int result;if (null == param.getId()) {result = this.baseMapper.insert(minioFileUploadInfo);} else {result = this.baseMapper.updateById(minioFileUploadInfo);}if (result == 0) {throw new MinioDemoException(MinioDemoExceptionTypes.USER_OPERATE_FAILED);}return ExtBeanUtils.doToDto(minioFileUploadInfo, MinioFileUploadInfoDTO.class);}@Overridepublic int updateFileStatusByFileMd5(MinioFileUploadInfoParam param) {MinioFileUploadInfo minioFileUploadInfo = this.baseMapper.selectOne(new LambdaQueryWrapper<MinioFileUploadInfo>().eq(MinioFileUploadInfo::getFileMd5, param.getFileMd5()));if (null == minioFileUploadInfo) {throw new MinioDemoException(MinioDemoExceptionTypes.DATA_NOT_EXISTED);}minioFileUploadInfo.setFileStatus(param.getFileStatus());minioFileUploadInfo.setFileUrl(param.getFileUrl());return this.baseMapper.updateById(minioFileUploadInfo);}
}
public interface MinioFileChunkUploadInfoService extends IService<MinioFileChunkUploadInfo> {boolean saveMinioFileChunkUploadInfo(MinioFileChunkUploadInfoParam chunkUploadInfoParam);List<MinioFileChunkUploadInfoDTO> listByFileMd5AndUploadId(String fileMd5, String uploadId);
}
@Service
public class MinioFileChunkUploadInfoServiceImplextends ServiceImpl<MinioFileChunkUploadInfoMapper, MinioFileChunkUploadInfo>implements MinioFileChunkUploadInfoService {@Overridepublic boolean saveMinioFileChunkUploadInfo(MinioFileChunkUploadInfoParam param) {List<MinioFileChunkUploadInfo> list = new ArrayList<>();for (int i = 0; i < param.getUploadUrls().size(); i++) {MinioFileChunkUploadInfo tempObj = new MinioFileChunkUploadInfo();tempObj.setChunkNumber(i + 1);tempObj.setFileMd5(param.getFileMd5());tempObj.setUploadId(param.getUploadId());tempObj.setExpiryTime(param.getExpiryTime());tempObj.setChunkUploadUrl(param.getUploadUrls().get(i));list.add(tempObj);}int result = this.baseMapper.insertBatchSomeColumn(list);return result != 0;}@Overridepublic List<MinioFileChunkUploadInfoDTO> listByFileMd5AndUploadId(String fileMd5, String uploadId) {List<MinioFileChunkUploadInfo> list = this.baseMapper.selectList(Wrappers.<MinioFileChunkUploadInfo>lambdaQuery().select(MinioFileChunkUploadInfo::getChunkUploadUrl).eq(MinioFileChunkUploadInfo::getFileMd5, fileMd5).eq(MinioFileChunkUploadInfo::getUploadId, uploadId));return ExtBeanUtils.doListToDtoList(list, MinioFileChunkUploadInfoDTO.class);}
}

至此 Entity、Mapper、Service 准备完毕

三、Minio分片实现

3.1 文件状态枚举

@Getter
public enum MinioFileStatus {UN_UPLOADED("UN_UPLOADED", "待上传"),UPLOADED("UPLOADED", "已上传"),UPLOADING("", "上传中");final String code;final String msg;MinioFileStatus(String code, String msg) {this.code = code;this.msg = msg;}
}

3.2 MinioService新增方法

public interface MinioService {/*** 初始化获取 uploadId** @param objectName  文件名* @param partCount   分片总数* @param contentType contentType* @return uploadInfo*/MinioUploadInfo initMultiPartUpload(String objectName,int partCount,String contentType);/*** 分片合并** @param objectName 文件名* @param uploadId   uploadId* @return region*/String mergeMultiPartUpload(String objectName, String uploadId);/*** 获取已上传的分片列表** @param objectName 文件名* @param uploadId   uploadId* @return 分片列表*/List<Integer> listUploadChunkList(String objectName, String uploadId);
}
@Component
@Slf4j
@RequiredArgsConstructor
public class MinioServiceImpl implements MinioService {public MinioUploadInfo initMultiPartUpload(String objectName, int partCount, String contentType) {HashMultimap<String, String> headers = HashMultimap.create();headers.put("Content-Type", contentType);String uploadId = "";List<String> partUrlList = new ArrayList<>();try {// 获取 uploadIduploadId = minioClient.getUploadId(minIoClientConfig.getBucketName(),null,objectName,headers,null);Map<String, String> paramsMap = new HashMap<>(2);paramsMap.put("uploadId", uploadId);for (int i = 1; i <= partCount; i++) {paramsMap.put("partNumber", String.valueOf(i));// 获取上传 urlString uploadUrl = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder()// 注意此处指定请求方法为 PUT,前端需对应,否则会报 `SignatureDoesNotMatch` 错误.method(Method.PUT).bucket(minIoClientConfig.getBucketName()).object(objectName)// 指定上传连接有效期// .expiry(paramConfig.getChunkUploadExpirySecond(), TimeUnit.SECONDS).extraQueryParams(paramsMap).build());partUrlList.add(uploadUrl);}} catch (Exception e) {log.error("initMultiPartUpload Error:" + e);return null;}// 过期时间 TODO 过期LocalDateTime expireTime = LocalDateTime.now().minusHours(1);MinioUploadInfo result = new MinioUploadInfo();result.setUploadId(uploadId);result.setExpiryTime(expireTime);result.setUploadUrls(partUrlList);return result;}/*** 分片合并** @param objectName 文件名* @param uploadId   uploadId*/public String mergeMultiPartUpload(String objectName, String uploadId) {// todo 最大1000分片 这里好像可以改吧Part[] parts = new Part[1000];int partIndex = 0;ListPartsResponse partsResponse = listUploadPartsBase(objectName, uploadId);if (null == partsResponse) {log.error("查询文件分片列表为空");throw new RuntimeException("分片列表为空");}for (Part partItem : partsResponse.result().partList()) {parts[partIndex] = new Part(partIndex + 1, partItem.etag());partIndex++;}ObjectWriteResponse objectWriteResponse;try {objectWriteResponse = minioClient.mergeMultipart(minIoClientConfig.getBucketName(), null, objectName, uploadId, parts, null, null);} catch (Exception e) {log.error("分片合并失败:" + e);throw new RuntimeException("分片合并失败:" + e.getMessage());}if (null == objectWriteResponse) {log.error("合并失败,合并结果为空");throw new RuntimeException("分片合并失败");}return objectWriteResponse.region();}/*** 获取已上传的分片列表** @param objectName 文件名* @param uploadId   uploadId*/public List<Integer> listUploadChunkList(String objectName, String uploadId) {ListPartsResponse partsResponse = listUploadPartsBase(objectName, uploadId);if (null == partsResponse) {return Collections.emptyList();}return partsResponse.result().partList().stream().map(Part::partNumber).collect(Collectors.toList());}private ListPartsResponse listUploadPartsBase(String objectName, String uploadId) {int maxParts = 1000;ListPartsResponse partsResponse;try {partsResponse = minioClient.listMultipart(minIoClientConfig.getBucketName(), null, objectName, maxParts, 0, uploadId, null, null);} catch (ServerException | InsufficientDataException | ErrorResponseException | NoSuchAlgorithmException |IOException | XmlParserException | InvalidKeyException | InternalException |InvalidResponseException e) {log.error("查询文件分片列表错误:{},uploadId:{}", e, uploadId);return null;}return partsResponse;}}

3.3 分片文件Service

public interface FileUploadService {/*** 获取分片上传信息** @param param 参数* @return Minio上传信息*/MinioUploadInfo getUploadId(GetMinioUploadInfoParam param);/*** 检查文件是否存在** @param md5 md5* @return true存在 false不存在*/MinioOperationResult checkFileExistsByMd5(String md5);/*** 查询已上传的分片序号** @param objectName 文件名* @param uploadId   uploadId* @return 已上传的分片序号列表*/List<Integer> listUploadParts(String objectName, String uploadId);/*** 分片合并** @param param 参数* @return url*/String mergeMultipartUpload(MergeMinioMultipartParam param);
}
@Slf4j
@Service
public class FileUploadServiceImpl implements FileUploadService {@Resourceprivate MinioService minioService;@Resourceprivate MinioFileUploadInfoService minioFileUploadInfoService;@Resourceprivate MinioFileChunkUploadInfoService minioFileChunkUploadInfoService;@Overridepublic MinioUploadInfo getUploadId(GetMinioUploadInfoParam param) {MinioUploadInfo uploadInfo;MinioFileUploadInfoDTO minioFileUploadInfo = this.minioFileUploadInfoService.getByFileMd5(param.getFileMd5());if (null == minioFileUploadInfo) {// 计算分片数量double partCount = Math.ceil(param.getFileSize() * 1.0 / param.getChunkSize());log.info("总分片数:" + partCount);uploadInfo = minioService.initMultiPartUpload(param.getFileName(), (int) partCount, param.getContentType());if (null != uploadInfo) {MinioFileUploadInfoParam saveParam = new MinioFileUploadInfoParam();saveParam.setUploadId(uploadInfo.getUploadId());saveParam.setFileMd5(param.getFileMd5());saveParam.setFileName(param.getFileName());saveParam.setTotalChunk((int) partCount);saveParam.setFileStatus(MinioFileStatus.UN_UPLOADED.getCode());// 保存文件上传信息MinioFileUploadInfoDTO minioFileUploadInfoDTO = minioFileUploadInfoService.saveMinioFileUploadInfo(saveParam);log.info("文件上传信息保存成功 {}", JSON.toJSONString(minioFileUploadInfoDTO));MinioFileChunkUploadInfoParam chunkUploadInfoParam = new MinioFileChunkUploadInfoParam();chunkUploadInfoParam.setUploadUrls(uploadInfo.getUploadUrls());chunkUploadInfoParam.setUploadId(uploadInfo.getUploadId());chunkUploadInfoParam.setExpiryTime(uploadInfo.getExpiryTime());chunkUploadInfoParam.setFileMd5(param.getFileMd5());chunkUploadInfoParam.setFileName(param.getFileName());// 保存分片上传信息boolean chunkUploadResult = minioFileChunkUploadInfoService.saveMinioFileChunkUploadInfo(chunkUploadInfoParam);log.info("文件分片信息保存{}", chunkUploadResult ? "成功" : "失败");}return uploadInfo;}// 查询分片上传地址List<MinioFileChunkUploadInfoDTO> list = minioFileChunkUploadInfoService.listByFileMd5AndUploadId(minioFileUploadInfo.getFileMd5(), minioFileUploadInfo.getUploadId());List<String> uploadUrlList = list.stream().map(MinioFileChunkUploadInfoDTO::getChunkUploadUrl).collect(Collectors.toList());uploadInfo = new MinioUploadInfo();uploadInfo.setUploadUrls(uploadUrlList);uploadInfo.setUploadId(minioFileUploadInfo.getUploadId());return uploadInfo;}@Overridepublic MinioOperationResult checkFileExistsByMd5(String md5) {MinioOperationResult result = new MinioOperationResult();MinioFileUploadInfoDTO minioFileUploadInfo = this.minioFileUploadInfoService.getByFileMd5(md5);if (null == minioFileUploadInfo) {result.setStatus(MinioFileStatus.UN_UPLOADED.getCode());return result;}// 已上传if (Objects.equals(minioFileUploadInfo.getFileStatus(), MinioFileStatus.UPLOADED.getCode())) {result.setStatus(MinioFileStatus.UPLOADED.getCode());result.setUrl(minioFileUploadInfo.getFileUrl());return result;}// 查询已上传分片列表并返回已上传列表List<Integer> chunkUploadedList = listUploadParts(minioFileUploadInfo.getFileName(), minioFileUploadInfo.getUploadId());result.setStatus(MinioFileStatus.UPLOADING.getCode());result.setChunkUploadedList(chunkUploadedList);return result;}@Overridepublic List<Integer> listUploadParts(String objectName, String uploadId) {return minioService.listUploadChunkList(objectName, uploadId);}@Overridepublic String mergeMultipartUpload(MergeMinioMultipartParam param) {String result = minioService.mergeMultiPartUpload(param.getFileName(), param.getUploadId());if (!StringUtils.isBlank(result)) {MinioFileUploadInfoParam fileUploadInfoParam = new MinioFileUploadInfoParam();fileUploadInfoParam.setFileUrl(result);fileUploadInfoParam.setFileMd5(param.getMd5());fileUploadInfoParam.setFileStatus(MinioFileStatus.UPLOADED.getCode());// 更新状态int updateRows = minioFileUploadInfoService.updateFileStatusByFileMd5(fileUploadInfoParam);log.info("update file by file md5 updated count {}", updateRows);}return result;}
}

3.4 Controller新增

@RequestMapping(value = "file")
@RestController
public class FileController {@Resourceprivate FileUploadService fileUploadService;@PostMapping("/upload")public R getUploadId(@Validate @RequestBody GetMinioUploadInfoParam param) {MinioUploadInfo minioUploadId = fileUploadService.getUploadId(param);return R.ok().setData(minioUploadId);}@GetMapping("/upload/check")public R checkFileUploadedByMd5(@RequestParam("md5") String md5) {return R.ok().setData(fileUploadService.checkFileExistsByMd5(md5));}@PostMapping("/upload/merge")public R mergeUploadFile(@Validated MergeMinioMultipartParam param) {String result = fileUploadService.mergeMultipartUpload(param);if (StringUtils.isEmpty(result)) {throw new MinioDemoException(MinioDemoExceptionTypes.CHUNK_MERGE_FAILED);}return R.ok().setData(result); // url}
}

3.5 文件分片上传测试

在这里插入图片描述
在这里插入图片描述

select * from minio_file_upload_info;

在这里插入图片描述

select * from minio_chunk_upload_info;

在这里插入图片描述

FAQ

  • 上传失败,code码为403,请同步minio服务器时间。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/419064.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【JavaEE Spring】SpringBoot 日志

SpringBoot 日志 1. 日志概述2. 日志使用2.1 打印⽇志2.1.1 在程序中得到⽇志对象2.1.2 使⽤⽇志对象打印⽇志 2.2 ⽇志框架介绍2.2.1 ⻔⾯模式(外观模式)2.2.2 SLF4J 框架介绍 2.3 ⽇志格式的说明2.4 ⽇志级别2.4.1 ⽇志级别的分类2.4.2 ⽇志级别的使⽤ 2.5 ⽇志配置2.5.1 配置…

Docker-Compose.yml 指南:让容器编排更简单

Docker-Compose.yml 指南&#xff1a;让容器编排更简单 引言Docker Compose 简介什么是 Docker Compose&#xff1f;Docker Compose 与 Docker 的区别 核心指令解析versionservicesnetworksvolumesbuildports其他常用指令 实战案例&#xff1a;使用 Docker Compose 搭建多容器应…

高性能前端UI库 SolidJS | 超棒 NPM 库

SolidJS是一个声明式的、高效的、编译时优化的JavaScript库&#xff0c;用于构建用户界面。它的核心特点是让你能够编写的代码既接近原生JavaScript&#xff0c;又能够享受到现代响应式框架提供的便利。 SolidJS的设计哲学强调了性能与简洁性。它不使用虚拟DOM&#xff08;Vir…

HarmonyOS 应用开发入门

HarmonyOS 应用开发入门 前言 DevEco Studio Release版本为&#xff1a;DevEco Studio 3.1.1。 Compile SDK Release版本为&#xff1a;3.1.0&#xff08;API 9&#xff09;。 构建方式为 HVigor&#xff0c;而非 Gradle。 最新版本已不再支持 &#xff08;”Java、JavaScrip…

ckman:非常好用的ClickHouse可视化集群运维工具

概述 什么是ckman ckman&#xff0c;全称是ClickHouse Management Console&#xff0c; 即ClickHouse管理平台。它是由擎创科技数据库团队主导研发的一款用来管理和监控ClickHouse集群的可视化运维工具。目前该工具已在github上开源&#xff0c;开源地址为&#xff1a;github…

vue路由-全局前置守卫

1. 介绍 详见&#xff1a;全局前置守卫网址 使用场景&#xff1a; 对于支付页&#xff0c;订单页等&#xff0c;必须是登录的用户才能访问的&#xff0c;游客不能进入该页面&#xff0c;需要做拦截处理&#xff0c;跳转到登录页面 全局前置守卫的原理&#xff1a; 全局前置…

中期国际1.18黄金市场分析:零售销售强劲增长,美联储降息可能性大幅降低!

金价在周四下跌&#xff0c;其中一个主要原因是美国国债收益率的持续上升。此外&#xff0c;强劲的美国零售销售报告也对金价造成了影响&#xff0c;该报告显示零售销售额大幅上涨&#xff0c;超出预期值&#xff0c;这使得美联储3月份降息的可能性大幅降低。 12月份的消费者价…

树的一些经典 Oj题 讲解

关于树的遍历 先序遍历 我们知道 树的遍历有 前序遍历 中序遍历 后序遍历 然后我们如果用递归的方式去解决&#xff0c;对我们来说应该是轻而易举的吧&#xff01;那我们今天要讲用迭代&#xff08;非递归&#xff09;实现 树的相关遍历 首先呢 我们得知道 迭代解法 本质上也…

彩色图像处理之伪彩色图像处理的python实现——数字图像处理

原理 伪彩色图像处理是一种多源信息融合的可视化方法。 处理对象:伪彩色图像处理的对象是多波段遥感图像,例如近红外带、红外带和可见光图像等。 原理:选择不同波段的原始图像作为新的三原色通道(如近红外为红色通道),按RGB模式合成伪彩色图像。 目的:利用不同波段信息融合,实…

力扣645.错误的集合

一点一点地刷&#xff0c;慢慢攻克力扣&#xff01;&#xff01; 王子公主请看题 集合 s 包含从 1 到 n 的整数。不幸的是&#xff0c;因为数据错误&#xff0c;导致集合里面某一个数字复制了成了集合里面的另外一个数字的值&#xff0c;导致集合 丢失了一个数字 并且 有一个数…

视频监控需求记录

记录一下最近要做的需求&#xff0c;我个人任务还是稍微比较复杂的 需求&#xff1a;需要实现一个视频实时监控、视频回放、视频设备管理&#xff0c;以上都是与组织架构有关 大概的界面长这个样子 听着需求好像很简单&#xff0c;但是~我们需要在一个界面上显示两个厂商的视…

在WIN从零开始在QMUE上添加一块自己的开发板(一)

文章目录 一、前言二、源码编译&#xff08;一&#xff09;安装Msys2&#xff08;二&#xff09;配置GCC工具链&#xff08;三&#xff09;安装QEMU构建依赖&#xff08;四&#xff09;下载编译QEMU源码 二、QUME编程基础&#xff08;一&#xff09;QOM机制&#xff08;二&…