简体   繁体   English

在 zip 中从 s3 下载多个文件的正确方法?

[英]Proper way to download multiple files from s3 on fly in zip?

I want to download multiple files from amazon s3 in zip file while click on single download button.我想在单击单个下载按钮的同时从 zip 文件中从亚马逊 s3 下载多个文件。

I want to achieve this using php or JavaScript.我想使用 php 或 JavaScript 来实现这一点。

I went over many solutions such as: Use recursive in aws cli, Zipstream on fly, but I didn't get any proper solution.我研究了许多解决方案,例如:在 aws cli 中使用递归、动态 Zipstream,但我没有得到任何适当的解决方案。

Do you have any idea for another, more efficient solution?您对另一种更有效的解决方案有什么想法吗?

<?php

$urls = [
    'http://django-dev.s3.amazonaws.com/static/img/sample-store.jpg',
    'http://ds-assets.s3.amazonaws.com/baobao/feature/2020/video/movie_sample.mp4',
    'http://ds-assets.s3.amazonaws.com/briefing/mailmagazine/2019/0701/sample_g.jpg',
];

$mh = curl_multi_init();
$resources = [];

foreach ($urls as $key => $url) {
    $resources[$key]['fp'] = fopen(sys_get_temp_dir() . '/file' . $key, 'w+'); // file pointer
    $resources[$key]['ch'] = curl_init($url); // curl handle
    curl_setopt($resources[$key]['ch'], CURLOPT_FILE, $resources[$key]['fp']); 
    curl_setopt($resources[$key]['ch'], CURLOPT_FOLLOWLOCATION, true);
    curl_multi_add_handle($mh, $resources[$key]['ch']);
}

$running = null;
do {
    curl_multi_exec($mh, $running);
} while ($running);

$zip = new ZipArchive();
$zip->open(sys_get_temp_dir() . '/files.zip', ZIPARCHIVE::CREATE);

foreach ($resources as $key => $resource) {
    curl_multi_remove_handle($mh, $resource['ch']);
    curl_close($resource['ch']);
    fclose($resource['fp']);
    $zip->addFile(sys_get_temp_dir() . '/file' . $key, '/file' . $key);
}

curl_multi_close($mh);
$zip->close();
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename=files.zip');
header('Pragma: no-cache');
readfile(sys_get_temp_dir() . '/files.zip');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过javascript和zip下载多个AWS s3文件的正确方法 - Proper way to download multiple AWS s3 files via javascript and zip 以 Zip 文件的形式从 S3 存储桶下载多个文件 - Download multiple files from S3 bucket in the form of Zip file 如何在s3中压缩大文件然后下载 - How to zip large files in s3 and then download it “没有这样的文件或目录”:从S3下载多个文件 - 'no such file or directory': Download multiple files from S3 将多个文件下载为 zip - javascript - download multiple files as zip - javascript 无法使用 JS 从 S3 预签名 url 下载多个文件 - Not able to download multiple files from S3 pre-signed urls using JS 如何将 Zip 文件从 s3 存储桶下载到 node.js lambda 函数中的本地目录 - How to download Zip File from an s3 bucket to a local directory within a node.js lambda function 以zip格式从Node js的Amazon S3存储桶下载图像 - Download images from amazon s3 bucket in Node js in zip format 有没有办法让 zip 成为整个 s3 目录并在不下载的情况下将 zip 留在那里? - Is there a way to zip an entire s3 directory and leave the zip there without downloading it? 使用 Javascript 将多个文件下载为 zip 文件或文件夹中的文件? - Download multiple files as a zip or in a folder using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM