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()上传时,获取的不是标准哈希值