I have a Amazon S3 bucket with files and want to generate a force download zip-file on Amazon EC2 (With Amazon Linux, similar to CentOS/Redhat Linux).
I have all signed urls in an (post) array and right now I'am using this code.
<?php
ini_set('max_execution_time', 300); //300 seconds = 5 minutes
ini_set('memory_limit', '8192M');
$imageNumber = 0;
# create new zip opbject
$zip = new ZipArchive();
# create a temp file & open it
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
foreach ($_POST['text'] as $key => $value) {
//Get extension
$ext = pathinfo(parse_url($value, PHP_URL_PATH), PATHINFO_EXTENSION);
# download file
$download_file = file_get_contents($value);
#add it to the zip
$zip->addFromString(basename($imageNumber.'-image.'.$ext),$download_file);
$imageNumber++;
}
# close zip
$zip->close();
# send the file to the browser as a download
header('Content-disposition: attachment; filename=download.zip');
header('Content-type: application/zip');
readfile($tmp_file);
?>
My goal is to keep the costs on EC2 as low as possible and right now I just have 4GB on my instance. As you can see, I have increased the memory limit and the problem is that I have 50-100 files of about 5 MB each, which is a few hundred MB per Zip file which of course is a problem if several people generates a zip at the same time.
Is there a better solution (less memory use and as low cost as possible on EC2)?