[英]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.