package com.xxx.service.store.minio;
import com.xxx.exception.FileDeleteException;
import com.xxx.exception.FileReadException;
import com.xxx.exception.FileStoreException;
import io.minio.*;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import io.minio.messages.Item;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* minio对象存储操作模板,相比MinioClient提供了更简单的操作方法和异常包装
*/
@Component
public class MinioTemplate {
private static final Logger LOG = LoggerFactory.getLogger(MinioTemplate.class);
@Autowired(required = false)
private MinioClient minioClient;
// 保存
public void putObject(String bucketName, String objectName, InputStream in) throws FileStoreException {
try {
minioClient.putObject(PutObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.stream(in, in.available(), -1)
.build());
} catch (Exception e) {
throw new FileStoreException(e);
}
}
// 查询
public List<String> listObjects(String bucketName, String prefix) throws FileReadException {
try {
Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder()
.bucket(bucketName)
.prefix(prefix)
.recursive(true)
.build());
List<String> objectNameList = new ArrayList<>();
for (Result<Item> result : results) {
objectNameList.add(result.get().objectName());
}
return objectNameList;
} catch (Exception e) {
throw new FileReadException(e);
}
}
// 删除
public void removeObjects(String bucketName, List<String> objectNameList) throws FileDeleteException {
try {
List<DeleteObject> deleteObjects = objectNameList.stream()
.map(DeleteObject::new)
.collect(Collectors.toList());
Iterable<Result<DeleteError>> results = minioClient.removeObjects(RemoveObjectsArgs.builder()
.bucket(bucketName)
.objects(deleteObjects)
.build());
// minio处理批量删除的时候,采用的延迟执行,需要通过迭代返回结果
for (Result<DeleteError> errorResult : results) {
DeleteError error = errorResult.get();
if (LOG.isWarnEnabled()) {
LOG.warn("Error in deleting object " + error.objectName() + "; " + error.message());
}
}
} catch (Exception e) {
throw new FileDeleteException(e);
}
}
// 分片组合
public void composeObject(String sourceBucketName, List<String> sourceObjectNameList, String targetBucketName, String targetObjectName)
throws FileStoreException {
try {
List<ComposeSource> sources = sourceObjectNameList.stream()
.map(objectName -> ComposeSource.builder()
.bucket(sourceBucketName)
.object(objectName)
.build()
)
.collect(Collectors.toList());
minioClient.composeObject(ComposeObjectArgs.builder()
.bucket(targetBucketName)
.object(targetObjectName)
.sources(sources)
.build());
} catch (Exception e) {
throw new FileStoreException(e);
}
}
// 获取对象流
public InputStream getInputStream(String bucketName, String objectName) throws FileReadException {
try {
return minioClient.getObject(GetObjectArgs.builder()
.bucket(bucketName)
.object(objectName)
.build());
} catch (Exception e) {
throw new FileReadException(e);
}
}
}
05-28
2268
03-04
4672