首页 > 其他 > 详细

Minio Util

时间:2020-11-16 11:47:08      阅读:42      评论:0      收藏:0      [点我收藏+]

pom.xml 具体依赖版本去官网看吧,不过中文文档好像很久不更新了建议看英文。

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>7.1.4</version>
</dependency>

工具类中没有包含桶策略相关设置,可以读取出桶策略的json然后自己设置。类中包含了一些自定义的工具类,替换成自己的就可以了。


import com.core.utils.OptionalUtil;
import com.core.utils.StringUtil;
import com.core.utils.minio.exception.MinioException;
import io.minio.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.ArrayList;


/**
 * minio 文件服务器工具类
 *
 * @ClassName: MinioUtil
 */
@Component
public class MinioUtil {

    @Value("${minio.url}")
    private String url;
    @Value("${minio.port}")
    private Integer port;
    @Value("${minio.accessKey}")
    private String accessKey;
    @Value("${minio.secretKey}")
    private String secretKey;
    @Value("${minio.secure}")
    private Boolean secure;


    private MinioClient buildMinIoClient() {
        return MinioClient.builder().endpoint(url, port, secure).credentials(accessKey, secretKey).build();
    }


    /**
     * 桶是否存在
     *
     * @param bucket 桶名 @NotNull
     * @return boolean true 存在; false 不存在;
     * @operation add
     * @date 14:00 2020/10/29
     **/
    public boolean bucketExists(String bucket) {
        try {
            return buildMinIoClient().bucketExists(BucketExistsArgs.builder().bucket(bucket).build());
        } catch (Exception ex) {
            throw new MinioException(ex.getMessage(), ex);
        }
    }


    /**
     * 如果桶不存在则创建
     *
     * @param bucket 桶名 @NotNull
     * @return void
     * @operation add
     * @date 14:00 2020/10/29
     **/
    public void createBucketByNotExists(String bucket) {
        if (!bucketExists(bucket)) {
            try {
                buildMinIoClient().makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
            } catch (Exception ex) {
                throw new MinioException(ex.getMessage(), ex);
            }
        }
    }


    /**
     * 流式上传文件:桶不存在会抛异常
     *
     * @param bucket 桶名称 @NotNull
     * @param objectName 存储目标名称 @NotNull 多路径存储实例: package/subpackage/.../objectName 开头不要有/
     * @param is 输入流 @NotNull
     * @param objectSize 文件大小
     * @param partSize 分片大小
     * @param contentType 文本类型 默认类型:application/octet-stream
     * @return void
     * @operation add
     * @date 14:01 2020/10/29
     **/
    public void putObjectThrows(String bucket, String objectName, InputStream is, Long objectSize, Long partSize,
        String contentType) {
        if (!bucketExists(bucket)) {
            throw new MinioException("The bucket not exists.");
        }
        putObject(bucket, objectName, is, objectSize, partSize, contentType);
    }


    /**
     * 流式上传文件:桶不存在会创建桶
     *
     * @param bucket 桶名称 @NotNull
     * @param objectName 存储目标名称 @NotNull 多路径存储实例: package/subpackage/.../objectName 开头不要有/
     * @param is 输入流 @NotNull
     * @param objectSize 文件大小
     * @param partSize 分片大小
     * @param contentType 文本类型 默认类型:application/octet-stream
     * @return void
     * @operation add
     * @date 14:01 2020/10/29
     **/
    public void putObjectSafe(String bucket, String objectName, InputStream is, Long objectSize, Long partSize,
        String contentType) {
        createBucketByNotExists(bucket);
        putObject(bucket, objectName, is, objectSize, partSize, contentType);
    }


    /**
     * 根据文件地址上传文件:桶不存在抛出异常
     *
     * @param bucket 桶名称 @NotNull
     * @param objectName 存储目标名称 @NotNull 多路径存储实例: package/subpackage/.../objectName 开头不要有/
     * @param filePath 文件存储路径 @NotNull
     * @param contentType 文本类型 默认类型:application/octet-stream
     * @return void
     * @operation add
     * @date 14:01 2020/10/29
     **/
    public void uploadObjectThrows(String bucket, String objectName, String filePath, String contentType) {
        if (!bucketExists(bucket)) {
            throw new MinioException("The bucket not exists.");
        }
        try {
            buildMinIoClient().uploadObject(UploadObjectArgs.builder()
                .bucket(bucket)
                .object(objectName)
                .filename(filePath)
                .contentType(StringUtil.convertBlankToDef(contentType, "application/octet-stream"))
                .build());
        } catch (Exception ex) {
            throw new MinioException(ex.getMessage(), ex);
        }
    }


    /**
     * 获取文件流
     *
     * @param bucket 桶名称 @NotNull
     * @param objectName 存储目标名称 @NotNull
     * @return java.io.InputStream
     * @operation add
     * @date 14:39 2020/10/29
     **/
    public InputStream getObject(String bucket, String objectName) {
        try {
            return buildMinIoClient().getObject(GetObjectArgs.builder()
                .bucket(bucket)
                .object(objectName)
                .build());
        } catch (Exception ex) {
            throw new MinioException(ex.getMessage(), ex);
        }
    }


    /**
     * 字节形式读取文件
     *
     * @param bucket 桶名称 @NotNull
     * @param objectName 存储目标名称 @NotNull
     * @return java.io.InputStream
     * @operation add
     * @date 14:39 2020/10/29
     **/
    public byte[] getObjectWithByteArray(String bucket, String objectName) {
        ArrayList<byte[]> partList = new ArrayList<>();
        try {
            InputStream is = buildMinIoClient().getObject(GetObjectArgs.builder()
                .bucket(bucket).object(objectName).build());
            byte[] buffered = new byte[1024];
            int len;
            while ((len = is.read(buffered)) > 0) {
                byte[] temp = new byte[len];
                System.arraycopy(buffered, 0, temp, 0, len);
                partList.add(temp);
            }
        } catch (Exception ex) {
            throw new MinioException(ex.getMessage(), ex);
        }
        if (partList.isEmpty()) {
            return new byte[]{0};
        }
        Integer size = partList.stream().map(x -> x.length).reduce(0, Integer::sum);
        byte[] result = new byte[size];
        int currentIndex = 0;
        for (byte[] bytes : partList) {
            for (byte aByte : bytes) {
                result[currentIndex++] = aByte;
            }
        }
        return result;
    }


    /**
     * 删除文件
     *
     * @param bucket 桶名称 @NotNull
     * @param objectName 存储目标名称 @NotNull
     * @return void
     * @operation add
     * @date 10:49 2020/10/30
     **/
    public void removeFile(String bucket, String objectName) {
        try {
            buildMinIoClient().removeObject(RemoveObjectArgs.builder().bucket(bucket).object(objectName).build());
        } catch (Exception ex) {
            throw new MinioException(ex.getMessage(), ex);
        }
    }


    /**
     * 复制文件
     *
     * @param sourceBucket 源文件桶 @NotNull
     * @param sourceObject 源文件名 @NotNull
     * @param bucket 目标桶 @NotNull
     * @param object 目标文件 @NotNull
     * @return void
     * @operation add
     * @date 11:04 2020/10/30
     **/
    public void copyObject(String sourceBucket, String sourceObject, String bucket, String object) {
        try {
            CopySource source = CopySource.builder().bucket(sourceBucket).object(sourceObject).build();
            buildMinIoClient().copyObject(CopyObjectArgs.builder().bucket(bucket).object(object).source(source).build());
        } catch (Exception ex) {
            throw new MinioException(ex.getMessage(), ex);
        }
    }


    /**
     * 流式上传文件
     *
     * @param bucket 桶名称 @NotNull
     * @param objectName 存储目标名称 @NotNull 多路径存储实例: package/subpackage/.../objectName 开头不要有/
     * @param is 输入流 @NotNull
     * @param objectSize 文件大小
     * @param partSize 分片大小
     * @return void
     * @author GYP
     * @operation add
     * @date 14:01 2020/10/29
     **/
    private void putObject(String bucket, String objectName, InputStream is, Long objectSize, Long partSize,
        String contentType) {
        try {
            objectSize = OptionalUtil.getValueOrDef(objectSize, Long.valueOf(is.available()));
            partSize = OptionalUtil.getValueOrDef(partSize, -1L);
            buildMinIoClient().putObject(PutObjectArgs.builder()
                .bucket(bucket)
                .object(objectName)
                .stream(is, objectSize, partSize)
                .contentType(StringUtil.convertBlankToDef(contentType, "application/octet-stream"))
                .build());
        } catch (Exception ex) {
            throw new MinioException(ex.getMessage(), ex);
        }
    }
}

Minio Util

原文:https://www.cnblogs.com/knowledgemine/p/13984117.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!