阿里云OSS文件批量下载,打成zip压缩包,不进行落盘! - Go语言中文社区

阿里云OSS文件批量下载,打成zip压缩包,不进行落盘!


首先有这样一个需求:

    批量下载阿里云OSS上的文件。不进行落盘。(也就是中间过程不进行存储)

思路:直接从阿里云OSS上获取数据流,并转存为zip压缩包,返回到当前请求(request)的 response中。

@RequestMapping(value = "/download", method = RequestMethod.GET)
public void downloadSource(HttpServletResponse response) {
    List<String> list = new ArrayList<>();
    list.add("product/image/20.png");
    list.add("product/image/21.png");
    list.add("product/image/22.png");
    list.add("product/image/23.png");
    list.add("product/image/24.png");
    String zipFileName = "test";
    AliYunOssUtils.batchDownLoadOssFile(list,zipFileName,response);
}

/**
 * 阿里云API的内或外网域名  //替换成自己的
 */
private static final String ENDPOINT = “”;
/**
 * 阿里云API的密钥Access Key ID   //替换成自己的
 */
private static final String ACCESS_KEY_ID = “”;
/**
 * 阿里云API的密钥Access Key Secret   //替换成自己的
 */ 
private static final String ACCESS_KEY_SECRET = “”;
/**
 * 阿里云API的bucket名称    //替换成自己的
 */
private static final String BACKET_NAME = “”;

/**
 * 批量下载oss 文件 并打成zip 包 返回到response中
 *
 * @param fileNames oss上的文件名集合 如:product/image/3448275920.png
 * @param zipFileName 压缩包文件名
 * @param response  HttpServletResponse
 */
public static void batchDownLoadOssFile(List<String> fileNames, String zipFileName, HttpServletResponse response) {
    response.setCharacterEncoding("utf-8");
    response.setContentType("multipart/form-data");
    response.setHeader("Content-Disposition", "attachment;fileName=" + zipFileName + ".zip");
    BufferedInputStream bis = null;
    try {
        ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
        OSSClient ossClient = new OSSClient(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        int sortNum = 0;
        for (String fileName : fileNames) {
            Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
            GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(BACKET_NAME, fileName, HttpMethod.GET);
            // 设置过期时间。
            request.setExpiration(expiration);
            // 生成签名URL(HTTP GET请求)。
            URL signedUrl = ossClient.generatePresignedUrl(request);
            // 使用签名URL发送请求。
            OSSObject ossObject = ossClient.getObject(signedUrl, new HashMap<>());

            if (ossObject != null) {
                InputStream inputStream = ossObject.getObjectContent();
                byte[] buffs = new byte[1024 * 10];

                String zipFile = sortNum + "_" + fileName.substring(fileName.lastIndexOf("/") + 1);
                ZipEntry zipEntry = new ZipEntry(zipFile);
                zos.putNextEntry(zipEntry);
                bis = new BufferedInputStream(inputStream, 1024 * 10);

                int read;
                while ((read = bis.read(buffs, 0, 1024 * 10)) != -1) {
                    zos.write(buffs, 0, read);
                }
                ossObject.close();
            }
            sortNum++;
        }
        zos.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        //关闭流
        try {
            if (null != bis) {
                bis.close();
            }
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

要注意的是 传入的文件名 不能重复。

此处的文件名也就是阿里云OSS上的文件名,直接从图片链接中截取就好了。测试可以直接去阿里云官网自己的OSS这个地方复制。 如下图:

 好啦。拜拜!~

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_41942909/article/details/95538687
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-07 23:01:19
  • 阅读 ( 1637 )
  • 分类:职场

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢