阿里云OSS 列出所有文件 并导出所有文件到指定目录

/**
     * 列出所有文件 并导出所有文件到指定目录
     *
     * @param prefix        oss目录
     * @param directoryPath 本地目录
     * @throws IOException 抛出io 异常
     */
    public static void listFiles(String prefix, String directoryPath) throws IOException {
        OSS ossClient = getOssClient();
        boolean flag;
        String marker = "";
        do {
            ListObjectsRequest lor = new ListObjectsRequest();
            //指定目录
            lor.setPrefix(prefix);
            lor.setBucketName(BUCKET_NAME);
            //
            lor.setMarker(marker);
            //分页大小
            lor.setMaxKeys(100);
            ObjectListing ol = ossClient.listObjects(lor);
            for (OSSObjectSummary o : ol.getObjectSummaries()) {
                System.out.println(" - " + o.getKey() + "  " + "(size = " + o.getSize() + ")");
                //
                final String path = directoryPath + o.getKey();
                //获取文件对象
                GetObjectRequest gor = new GetObjectRequest(BUCKET_NAME, o.getKey());
                //截取目录 如果目录不存在则创建
                String directoryStr = path.substring(0, path.lastIndexOf("/"));
                File directory = new File(directoryStr);
                if (!directory.exists()) {
                    directory.mkdirs();
                }
                try (//获取文件流  使用高速缓存 + 数组复制 最大效率输出文件
                     BufferedInputStream bis = new BufferedInputStream(ossClient.getObject(gor).getObjectContent());
                     BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path))) {
                    int size;
                    byte[] bytes = new byte[1024];
                    while ((size = bis.read(bytes)) != -1) {
                        bos.write(bytes, 0, size);
                    }
                    bos.flush();
                }
            }
            //当前页的最后一个文件
            marker = ol.getNextMarker();
            //是否还有文件
            flag = ol.isTruncated();

        } while (flag);

        ossClient.shutdown();
    }

    public static void main(String[] args) throws IOException {
        listFiles("ueditor/", "E:/");
    }
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

等一场春雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值