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()上传时,获取的不是标准哈希值
相关推荐
程序员清风14 小时前
程序员兼职必看:靠谱软件外包平台挑选指南与避坑清单!
java·后端·面试
皮皮林55115 小时前
利用闲置 Mac 从零部署 OpenClaw 教程 !
java
华仔啊20 小时前
挖到了 1 个 Java 小特性:var,用完就回不去了
java·后端
SimonKing20 小时前
SpringBoot整合秘笈:让Mybatis用上Calcite,实现统一SQL查询
java·后端·程序员
日月云棠2 天前
各版本JDK对比:JDK 25 特性详解
java
用户8307196840822 天前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
JavaGuide2 天前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code
IT探险家2 天前
Java 基本数据类型:8 种原始类型 + 数组 + 6 个新手必踩的坑
java
花花无缺2 天前
搞懂new 关键字(构造函数)和 .builder() 模式(建造者模式)创建对象
java
用户908324602732 天前
Spring Boot + MyBatis-Plus 多租户实战:从数据隔离到权限控制的完整方案
java·后端