Mino基于JAVA API 对文件对象的基础操作封装


/**
 * @author qianxm
 * @date 2020/6/30 16:58
 */
@Log4j
public class MinioUtils {

    public static void fileUpload(String endpoint ,String accessKey,
                                  String secrtKey,String bucketName,
                                  String filename,String filepath) throws Exception{
        try {

            // 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象
            MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);

            // 检查存储桶是否已经存在
            boolean isExist = minioClient.bucketExists(bucketName);
            if(isExist) {
                System.out.println("Bucket already exists.");
            } else {
                // 创建一个名为asiatrip的存储桶,用于存储照片的zip文件。
                minioClient.makeBucket(bucketName);
            }

            // 使用putObject上传一个文件到存储桶中。
            minioClient.putObject(bucketName,filename, filepath);
            log.debug(filepath + "is successfully uploaded as" + filename +"to `" + bucketName + "` bucket.");
            System.out.println("/home/user/Photos/asiaphotos.zip is successfully uploaded as asiaphotos.zip to `asiatrip` bucket.");
        } catch(Exception e) {
           log.error("Error occurred: "+e.getMessage(), e);
           throw new Exception("文件上传失败!", e);

        }
    }

    /**
     * 下载文件到本地.
     * @param endpoint 访问地址
     * @param accessKey 用户
     * @param secrtKey 密码
     * @param bucketName 桶名称
     * @param filename 服务端文件名称
     * @param result 本地文件
     */
    public static void fileDownload(String endpoint ,String accessKey,
                                    String secrtKey,String bucketName,
                                    String filename,String result) throws Exception{
        try {

            // 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象
            MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);

            // 检查存储桶是否已经存在
            boolean isExist = minioClient.bucketExists(bucketName);
            if(isExist) {
                throw new Exception(bucketName + " does not exist");
            }
            File file = new File(result);
            String parent = file.getParent();
            if(!FileUtils.isExists(parent)) {
                FileUtils.createDirs(parent);
            }

            minioClient.getObject(bucketName, filename,result);

        } catch(MinioException | NoSuchAlgorithmException | IOException | InvalidKeyException | XmlPullParserException e) {
            throw new Exception("Error occurred: " + e);
        }
    }

    /**
     * 下载文件,通过读取流下载到本地;默认编码gbk不推荐使用.
     * @param endpoint minio访问地址url
     * @param accessKey 用户名
     * @param secrtKey 密码
     * @param bucketName 桶名称
     * @param filename Minio文件 包括路径
     * @param result 本地文件(如果有父级目录请自行创建)
     */
    public static void download(String endpoint ,String accessKey,
                                String secrtKey,String bucketName,
                                String filename,String result){
        OutputStream out = null;
        InputStream in = null;

        try{
            // 使用MinIO服务的URL,端口,Access key和Secret key创建一个MinioClient对象
            MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);

            // 检查存储桶是否已经存在
            boolean isExist = minioClient.bucketExists(bucketName);
            if(!isExist) {
                throw new Exception(bucketName + " does not exist");

            }


            File file = new File(result);
            String parent = file.getParent();
            if(!FileUtils.isExists(parent)) {
                FileUtils.createDirs(parent);
            }
            // 使用putObject获取一个文件到存储桶中。
            in = minioClient.getObject(bucketName,filename);
            int len = 0;
            byte[] buffer = new byte[1024];
            out = new FileOutputStream(result);

            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }


    /**
     * 根据文件字节其实位置读取指定长度的字符;如果offset=0;默认读取全部.
     * @param endpoint 访问地址
     * @param accessKey 用户名
     * @param secrtKey 密码
     * @param bucketName 桶名
     * @param filename 保存在minio文件名
     * @param offset 起始字节数
     * @param length 读取长度
     * @return 返回结果
     */
    public static List<String> read(String endpoint , String accessKey,
                                    String secrtKey, String bucketName,
                                    String filename, long offset, Long length) throws Exception{
        InputStream in = null;
        List<String> result = new ArrayList<>();
        try {
            MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);

            minioClient.statObject(bucketName,filename);
            // 检查存储桶是否已经存在
            boolean isExist = minioClient.bucketExists(bucketName);
            if(!isExist) {
                throw new Exception(bucketName + " does not exist");
            }

            // 使用putObject上传一个文件到存储桶中。
            in = minioClient.getObject(bucketName, filename,offset,length);
            // 读取输入流直到EOF并打印到控制台。
            byte[] buf = new byte[65535];
            int bytesRead;
            while ((bytesRead = in.read(buf)) >= 0) {
                result.add(new String(buf,0,bytesRead));
            }


            in.close();
        }catch (Exception e) {
           throw new Exception(e);
        }
        return result;
    }


    /**
     * 从offset行读到length行.
     * @param endpoint 访问地址
     * @param accessKey 用户名
     * @param secrtKey 密码
     * @param bucketName 桶名称
     * @param filename 文件名
     * @param offset 开始行
     * @param length 结束行
     * @return 返回结果
     */
    public static List<String> readByLine(String endpoint , String accessKey,
                                          String secrtKey, String bucketName,
                                          String filename,int offset, int length) throws Exception{
        InputStream in = null;
        List<String> result = new ArrayList<>();
        List<String> list = new ArrayList<>();
        try {
            MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);

            // 检查存储桶是否已经存在
            boolean isExist = minioClient.bucketExists(bucketName);
            if(!isExist) {
                throw new Exception(bucketName + " does not exist");
            }
            minioClient.statObject(bucketName, filename);

            // 使用putObject上传一个文件到存储桶中。
            in = minioClient.getObject(bucketName, filename);
            // 读取输入流直到EOF并打印到控制台。
            byte[] buf = new byte[1024];
            int bytesRead;

            while ((bytesRead = in.read(buf)) >= 0) {
                result.add(new String(buf, 0, bytesRead));
            }


            for (String line : result) {
                String[] lines = line.split("\r\n", -1);
                list.addAll(new ArrayList<String>(Arrays.asList(lines)));
            }

            in.close();
        }catch (Exception e) {
            throw new Exception(e);
        }

        return list.subList(offset-1,length);
    }

    /**
     * 列出桶里所有对象,即桶下所有文件.
     * @param endpoint 访问地址
     * @param accessKey 用户名
     * @param secrtKey 密码
     * @param bucketName 桶名称
     */
    public static List<String>  listBucketNameObject(String endpoint , String accessKey,
                                             String secrtKey, String bucketName) throws Exception{
        List<String> list = new ArrayList<>();
        try {

            MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);

            // 检查存储桶是否已经存在
            boolean isExist = minioClient.bucketExists(bucketName);
            if(isExist) {
                System.out.println("Bucket already exists.");
            }
            // 检查'mybucket'是否存在。
            boolean found = minioClient.bucketExists(bucketName);
            if (found) {
                // 列出'my-bucketname'里的对象
                Iterable<Result<Item>> myObjects = minioClient.listObjects(bucketName);
                for (Result<Item> result : myObjects) {
                    Item item = result.get();
                    list.add(item.objectName());
                   log.debug(item.lastModified() + ", " + item.size() + ", " + item.objectName());
                }
            } else {

                throw new Exception(bucketName + " does not exist");
            }

        } catch (Exception e) {
            throw new Exception(e);
        }
        return list;
    }

    /**
     * 列出前缀里的所有对象.
     * @param endpoint 访问地址
     * @param accessKey 用户
     * @param secrtKey 密码
     * @param bucketName 桶名称
     * @param prefix 前缀 文件路径
     * @param recursive true表示递归查找
     * @return 返回结果
     */
    public static List<String>  listObject(String endpoint , String accessKey,
                                           String secrtKey, String bucketName,
                                           String prefix,boolean recursive)
    throws Exception{
        List<String> file = new ArrayList<>();
        try {

            MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);


            // 检查bucketName是否存在。
            boolean found = minioClient.bucketExists(bucketName);
            if (found) {
                // 列出prefix里的对象
                Iterable<Result<Item>> myObjects = minioClient.listObjects(bucketName,prefix,recursive,false);
                for (Result<Item> result : myObjects) {
                    Item item = result.get();
                    log.debug(item.lastModified() + ", " + item.size() + ", " + item.objectName() + "," + item.objectSize());
                    file.add(item.objectName());
                }
            } else {
                throw new Exception(bucketName + " does not exist");
            }

        } catch (Exception e) {
            throw new Exception(e);
        }
        return file;
    }

    // 将前缀中的所有对象都下载到本地目录

    /**
     * 批量下载文件.
     * @param endpoint 访问地址
     * @param accessKey 账号
     * @param secrtKey 密码
     * @param bucketName 同名称
     * @param prefix 前缀名称 即桶中对象文件的前缀路径
     * @param resultPath 本地路径
     */
    public static void downFiles(String endpoint , String accessKey,
                                 String secrtKey, String bucketName,
                                 String prefix,String resultPath)
    throws Exception{
        try {

            MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);

            // 检查存储桶是否已经存在

            boolean found = minioClient.bucketExists(bucketName);
            // 如果目标文件不存在创建目录
            if (found) {
                // 列出'my-bucketname'里的对象
                Iterable<Result<Item>> myObjects = minioClient.listObjects(bucketName,prefix,true,false);
                for (Result<Item> result : myObjects) {
                    Item item = result.get();
                    String objectName = item.objectName();
                    String localfile = resultPath + File.separator +objectName.replaceAll(prefix,"");
                    File file = new File(localfile);
                    String parent = file.getParent();
                    if(!FileUtils.isExists(parent)) {
                        FileUtils.createDirs(parent);
                    }
                    minioClient.getObject(bucketName,objectName,localfile);
                }
            } else {
                throw new Exception(bucketName +" does not exist");
            }
        } catch (MinioException e) {
           throw new MinioException("Error occurred: " + e);
        } catch (Exception e) {
            throw new Exception(e);
        }

    }

    /**
     * 拷贝文件,将桶1中的文件拷贝到另一个桶中,不修改名字.
     * @param endpoint 访问地址
     * @param accessKey 用户
     * @param secrtKey 密码
     * @param bucketName 复制的桶
     * @param object 要复制的对象文件
     * @param destBucketName 目标桶
     */
    public static void copy(String endpoint , String accessKey,
                            String secrtKey, String bucketName,
                            String object,String destBucketName)
    throws Exception{
        try {

            MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);

            if(!minioClient.bucketExists(destBucketName)) {
                minioClient.makeBucket(destBucketName);
            }
            // 检查'mybucket'是否存在。
            boolean found = minioClient.bucketExists(bucketName);
            // 如果目标文件不存在创建目录
            if (found) {
                minioClient.copyObject(bucketName,object,destBucketName);
               log.debug("拷贝成功");

            } else {
                throw new Exception( bucketName + " does not exist");
            }
        } catch (MinioException e) {
            throw new Exception("Error occurred: " + e);
        } catch (Exception e) {
            throw new Exception(e);
        }

    }


    /**
     * 拷贝文件,将桶1中的文件拷贝到另一个桶中,修改名称.
     * @param endpoint 访问地址
     * @param accessKey 用户
     * @param secrtKey 密码
     * @param bucketName 复制的桶
     * @param object 要复制的对象文件
     * @param destBucketName 目标桶
     * @param destBucketObjectName  目标文件
     */
    public static void copy(String endpoint , String accessKey,
                            String secrtKey, String bucketName,
                            String object,String destBucketName,
                            String destBucketObjectName)
    throws Exception{
        try {

            MinioClient minioClient = new MinioClient(endpoint, accessKey, secrtKey);

            boolean found = minioClient.bucketExists(bucketName);
            // 如果目标文件不存在创建目录
            if (found) {
                minioClient.copyObject(bucketName,object,destBucketName,destBucketObjectName);
                System.out.println("拷贝成功");

            } else {
                throw new Exception( bucketName + " does not exist");
            }
        } catch (MinioException e) {
            throw new Exception("Error occurred: " + e);
        } catch (Exception e) {
            throw new Exception(e);
        }

    }
}

你可能感兴趣的:(Minio,java)