MinioTemplate MinIO工具类

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);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值