Java基于minio的视频下载
1、前端代码(Vue)
downloadFile(row) { downloadVideo(row, this.form).then((resp) => { const data = resp.data; if (data.data) { let path = data.data.url; window.location.href = path; this.$message.success(data.message); } else { this.$message.error(data.message || "下载视频失败"); } }); },
2、后端代码
Minio客户端:
import com.google.common.collect.Multimap; import io.minio.*; import io.minio.errors.*; import io.minio.messages.Part; import java.io.IOException; import java.io.InputStream; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; /** * Created by TD on 2021/10/26 * 扩展 MinioClient <很多protected 修饰符的分片方法,MinioClient实例对象无法使用,只能自定义类继承使用> * minio 大文件分片上传思路: * 1. 前端访问文件服务,请求上传文件,后端返回签名数据及uploadId * 2. 前端分片文件,携带签名数据及uploadId并发上传分片数据 * 3. 分片上传完成后,访问合并文件接口,后台负责合并文件。 */ public class PearlMinioClient extends MinioClient { public PearlMinioClient(MinioClient client) { super(client); } /** * 创建分片上传请求 * * @param bucketName 存储桶 * @param region 区域 * @param objectName 对象名 * @param headers 消息头 * @param extraQueryParams 额外查询参数 */ @Override public CreateMultipartUploadResponse createMultipartUpload(String bucketName, String region, String objectName, Multimap<String, String> headers, Multimap<String, String> extraQueryParams) throws NoSuchAlgorithmException, InsufficientDataException, IOException, InvalidKeyException, ServerException, XmlParserException, ErrorResponseException, InternalException, InvalidResponseException { return super.createMultipartUpload(bucketName, region, objectName, headers, extraQueryParams); } /** * 完成分片上传,执行合并文件 * * @param bucketName 存储桶 * @param region 区域 * @param objectName 对象名 * @param uploadId 上传ID * @param parts 分片 * @param extraHeaders 额外消息头 * @param extraQueryParams 额外查询参数 */ @Override public ObjectWriteResponse completeMultipartUpload(String bucketName, String region, String objectName, String uploadId, Part[] parts, Multimap<String, String> extraHeaders, Multimap<String, String> extraQueryParams) throws NoSuchAlgorithmException, InsufficientDataException, IOException, InvalidKeyException, ServerException, XmlParserException, ErrorResponseException, InternalException, InvalidResponseException { return super.completeMultipartUpload(bucketName, region, objectName, uploadId, parts, extraHeaders, extraQueryParams); } /** * 查询分片数据 * * @param bucketName 存储桶 * @param region 区域 * @param objectName 对象名 * @param uploadId 上传ID * @param extraHeaders 额外消息头 * @param extraQueryParams 额外查询参数 */ public ListPartsResponse listMultipart(String bucketName, String region, String objectName, Integer maxParts, Integer partNumberMarker, String uploadId, Multimap<String, String> extraHeaders, Multimap<String, String> extraQueryParams) throws NoSuchAlgorithmException, InsufficientDataException, IOException, InvalidKeyException, ServerException, XmlParserException, ErrorResponseException, InternalException, InvalidResponseException { return super.listParts(bucketName, region, objectName, maxParts, partNumberMarker, uploadId, extraHeaders, extraQueryParams); } public void putObject(String bucketName, String objectName, InputStream inputStream) throws IOException, ServerException, InsufficientDataException, ErrorResponseException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException { super.putObject( PutObjectArgs.builder() .bucket(bucketName) .object(objectName).stream(inputStream, inputStream.available(), -1) .build()); inputStream.close(); } }
获取带签名的临时下载地址:
@Slf4j @AllArgsConstructor @Component public class MinioTemplateNew { /** * MinIO 客户端 */ PearlMinioClient pearlMinioClient; MinioProp minioProp;/** * 返回临时带签名、过期时间一天、PUT请求方式的访问URL * * @param bucketName 桶名 * @param ossFilePath Oss文件路径 * @return */ @SneakyThrows public String getPresignedObjectUrlForPut(String bucketName, String ossFilePath, Map<String, String> queryParams) { return pearlMinioClient.getPresignedObjectUrl( GetPresignedObjectUrlArgs.builder() .method(Method.PUT) .bucket(bucketName) .object(ossFilePath) .expiry(60 * 60 * 24) .extraQueryParams(queryParams) .build()); } /** * 返回临时带签名、过期时间一天、Get请求方式的访问URL * * @param bucketName 桶名 * @param ossFilePath Oss文件路径 * @return */ @SneakyThrows public String getPresignedObjectUrlForGet(String bucketName, String ossFilePath, Map<String, String> queryParams) { return pearlMinioClient.getPresignedObjectUrl( GetPresignedObjectUrlArgs.builder() .method(Method.GET) .bucket(bucketName) .object(ossFilePath) .expiry(60 * 10) .extraQueryParams(queryParams) .build()); } }
Service层调用工具类方法,给前端返回临时下载路径:
// 生成临时路径过后,直接插入视频下载表,不用管是否下载成功、目前下载直接走的minio后端拿不到是否下载成功的状态 String url = uploadFileService.getFileTemporaryDownloadUrl(video_.getZipUploadFileId());
3、注意:需要把minio上的bucket桶修改为read-write权限