java minio 分片上传工具类与测试demo

java minio 分片上传工具类与测试demo

  • 工具类方法
    • [1、presignedUrl 获取文件上传的预授权url](#1、presignedUrl 获取文件上传的预授权url)
    • [2、compose 合并文件,并获取合并后的文件的哈希值](#2、compose 合并文件,并获取合并后的文件的哈希值)
    • [3、removeBatch 批量删除分片文件](#3、removeBatch 批量删除分片文件)
  • 完整demo
  • 说明

工具类方法

1、presignedUrl 获取文件上传的预授权url

java 复制代码
	public static String presignedUrl(String customBucket,String object){
		String newBucket = bucketName;
		if(StringUtils.isNotEmpty(customBucket)){
			newBucket = customBucket;
		}

		// 获取上传URL(客户端直接上传)
		String uploadUrl = null;
		try {
			initMinio(minioUrl, minioName,minioPass);
			// 检查存储桶是否已经存在
			if(minioClient.bucketExists(BucketExistsArgs.builder().bucket(newBucket).build())) {

			} else {
				// 创建存储桶
				minioClient.makeBucket(MakeBucketArgs.builder().bucket(newBucket).build());
			}

			uploadUrl = minioClient.getPresignedObjectUrl(
					GetPresignedObjectUrlArgs.builder()
							.method(Method.PUT)
							.bucket(newBucket)
							.object(object)
							.expiry(15, TimeUnit.MINUTES)  // 15分钟有效
							.build());
		} catch (ErrorResponseException e) {
			throw new RuntimeException(e);
		} catch (InsufficientDataException e) {
			throw new RuntimeException(e);
		} catch (InternalException e) {
			throw new RuntimeException(e);
		} catch (InvalidKeyException e) {
			throw new RuntimeException(e);
		} catch (InvalidResponseException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		} catch (XmlParserException e) {
			throw new RuntimeException(e);
		} catch (ServerException e) {
			throw new RuntimeException(e);
		}

		System.out.println("上传URL: " + uploadUrl);
		return uploadUrl;
	}

2、compose 合并文件,并获取合并后的文件的哈希值

java 复制代码
/**
	 * 合并文件并获取对象hash
	 * @param sourceObjectList
	 * @param customBucket
	 * @param object
	 * @return etag 对象的ETag(通常是MD5哈希)
	 */
	public static String compose(List<ComposeSource> sourceObjectList,String customBucket,String object){
		String newBucket = bucketName;
		if(StringUtils.isNotEmpty(customBucket)){
			newBucket = customBucket;
		}
		try {
			initMinio(minioUrl, minioName,minioPass);
			// 检查存储桶是否已经存在
			if(minioClient.bucketExists(BucketExistsArgs.builder().bucket(newBucket).build())) {

			} else {
				// 创建存储桶
				minioClient.makeBucket(MakeBucketArgs.builder().bucket(newBucket).build());
			}

			ObjectWriteResponse objectWriteResponse = minioClient.composeObject(
					ComposeObjectArgs.builder()
							.bucket(newBucket)
							.object(object)
							.sources(sourceObjectList)
							.build());
			return objectWriteResponse.etag();
		}catch (Exception e){
			e.printStackTrace();
		}
		return null;
	}

3、removeBatch 批量删除分片文件

java 复制代码
public static void removeBatch(String customBucket,List<String> objectList){
		String newBucket = bucketName;
		if(StringUtils.isNotEmpty(customBucket)){
			newBucket = customBucket;
		}

		// 获取上传URL(客户端直接上传)
		String uploadUrl = null;
		try {
			initMinio(minioUrl, minioName,minioPass);
			// 检查存储桶是否已经存在
			if(minioClient.bucketExists(BucketExistsArgs.builder().bucket(newBucket).build())) {

			} else {
				// 创建存储桶
				minioClient.makeBucket(MakeBucketArgs.builder().bucket(newBucket).build());
			}

			// 转换为DeleteObject列表
			List<DeleteObject> objects = objectList.stream()
					.map(DeleteObject::new)
					.collect(Collectors.toList());

			// 批量删除
			Iterable<Result<DeleteError>> results = minioClient.removeObjects(
					RemoveObjectsArgs.builder()
							.bucket(bucketName)
							.objects(objects)
							.build()
			);
			// 检查删除结果
			for (Result<DeleteError> result : results) {
				DeleteError error = result.get();
				if (error == null) {
					System.out.println("成功删除:" + error.objectName());
				} else {
					System.err.println("Error deleting object: " + error.objectName() +
							" - " + error.message());
				}
			}
		} catch (ServerException e) {
			throw new RuntimeException(e);
		} catch (InsufficientDataException e) {
			throw new RuntimeException(e);
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		} catch (InvalidKeyException e) {
			throw new RuntimeException(e);
		} catch (XmlParserException e) {
			throw new RuntimeException(e);
		} catch (InternalException e) {
			throw new RuntimeException(e);
		} catch (ErrorResponseException e) {
			throw new RuntimeException(e);
		} catch (InvalidResponseException e) {
			throw new RuntimeException(e);
		}
	}

完整demo

https://download.csdn.net/download/weixin_44957529/92640261

说明

为什么要用预授权url

  • 前端直接访问minio服务,减少后端服务器压力
  • 使用预授权url上传,能得到分片文件的标准哈希值,而使用MinioUtil.upload()上传时,获取的不是标准哈希值
相关推荐
戮戮33 分钟前
Spring Cloud Gateway 零拷贝参数校验:一种高性能网关架构实践
java·网络·架构·gateway
alengan1 小时前
cocos自动编译-Android自动出apk包
java·eclipse
漫霂1 小时前
二叉树的统一迭代遍历
java·算法
文静小土豆1 小时前
K8s 滚动更新在 Java 应用中的实践与优化
java·容器·kubernetes
HSunR1 小时前
java springboot3 后端 基础框架
java·开发语言
七夜zippoe1 小时前
Java技术未来展望:GraalVM、Quarkus、Helidon等新趋势探讨
java·开发语言·python·quarkus·graaivm·helidon
枫叶落雨2221 小时前
ClassPathXmlApplicationContext
java·开发语言
草莓熊Lotso1 小时前
【Linux 线程进阶】进程 vs 线程资源划分 + 线程控制全详解
java·linux·运维·服务器·数据库·c++·mysql
gelald1 小时前
Spring Boot - 自动配置原理
java·spring boot·后端
hssfscv1 小时前
软件设计师下午题六——Java的各种设计模式
java·算法·设计模式