Minio的工具类实现(封装了基础的minio的方法)

2023-12-26 18:59:00

Minio是一个基于Golang编写的开源对象存储套件,提供了单机和分布式部署方式,支持阿里云、Amazon?S3、MicroSoft Azure等云存储服务的对接。

这是本人写的操作Minio的基础方法封装 一键复制粘贴即可使用

@Component
public class MinioUtil {

    @Resource
    private MinioClient minioClient;

    /**
     * 创建桶
     * @param bucket
     * @throws Exception
     */
    public void createBucket(String bucket) throws Exception {
        //查看桶是否存在
        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        if (!exists){
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
        }
    }

    /**
     * 上传文件
     * @param inputStream
     * @param bucket
     * @param objectName
     * @throws Exception
     */
    public void uploadFile(InputStream inputStream,String bucket,String objectName) throws Exception{
        //上传文件
         minioClient.putObject(PutObjectArgs.builder().bucket(bucket).object(objectName)
                 .stream(inputStream,-1,Integer.MAX_VALUE).build());
    }

    /**
     * 获取所有的桶
     * @return
     * @throws Exception
     */
    public List<String> getAllBucket() throws Exception{
        List<Bucket> buckets = minioClient.listBuckets();
        return buckets.stream().map(Bucket::name).collect(Collectors.toList());
    }

    /**
     * 获取所有的问题件
     * @param bucket
     * @return
     * @throws Exception
     */
    public List<FileInfo> getAllFiles(String bucket) throws Exception {
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucket).build());
        List<FileInfo> fileInfoList = new ArrayList<>();
        for (Result<Item> result: results) {
            FileInfo fileInfo = new FileInfo();
            Item item = result.get();
            //塞入属性
            fileInfo.setFileName(item.objectName());
            fileInfo.setDirectoryFlag(item.isDir());
            fileInfo.setEtag(item.etag());
            fileInfoList.add(fileInfo);
        }
        return fileInfoList;
    }

    /**
     * 下载文件
     * @param bucket
     * @param objectName
     * @return
     * @throws Exception
     */
    public InputStream downLoad(String bucket,String objectName) throws Exception{
        return minioClient.getObject(GetObjectArgs.builder().bucket(bucket).object(objectName).build());
    }

    /**
     * 删除桶
     * @param bucket
     * @throws Exception
     */
    public void deleteBucket(String bucket) throws Exception{
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucket).build());
    }

    /**
     * 删除文件
     * @param objectName
     * @throws Exception
     */
    public void deleteObject(String objectName) throws Exception{
        minioClient.removeObject(RemoveObjectArgs.builder().object(objectName).build());
    }
}

文件类的实现

/**
 * 文件类
 */
public class FileInfo {
    private String fileName;

    private Boolean directoryFlag;

    private String etag;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public Boolean getDirectoryFlag() {
        return directoryFlag;
    }

    public void setDirectoryFlag(Boolean directoryFlag) {
        this.directoryFlag = directoryFlag;
    }

    public String getEtag() {
        return etag;
    }

    public void setEtag(String etag) {
        this.etag = etag;
    }
}

文章来源:https://blog.csdn.net/Superkom666/article/details/135227320
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。