阿里雲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這個地方複製。 如下圖:

 好啦。拜拜!~

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章