We have a problem downloading large .zip files from GCE, it is either memory limit (512 MB), or response size limit (32 MB).
We could increase memory limit, but we already have 1 GB .zip and it can be 10 GB next time.
Response size limit cannot be increased.
The goal: download product images archived as .zip.
Images vary from 1 MB to 10+ MB, so e.g. 100+ products .zip would be 1 GB in size. In the future there will be catalogs of 1K products, so the size can be 10 GB+.
Project is on Laravel 6.
I've already tried various solutions without success:
ZipStream
. Result: Response size limit triggered.--
Code usingZipStream
:
$options->setSendHttpHeaders(true);
//$options->setContentType('application/octet-stream');
$options->setZeroHeader(true);
$zipName = time().'.zip';
// create a new zipstream object
$zip = new ZipStream($zipName, $options);
// Turned off deflate
$fileOptions = new FileOptions();
$fileOptions->setMethod(MethodOptions::STORE());
foreach($files as $file) {
$zip->addFileFromStream($file['name'], Storage::readStream($file['src'], $fileOptions));
}
// finish the zip stream
$zip->finish();
This way it is OK with memory limits, but I get response size (32MB) error which not sure how to overcome.
I've tried saving to GCS, but without any luck, it runs out of memory each time. Even if I could save successfully, it runs out of memory during download, e.g.:
$fs = Storage::getDriver();
$metaData = $fs->getMetadata($filePath);
$stream = $fs->readStream($filePath);
return response()->streamDownload(function () use ($stream) {
while(ob_get_level() > 0) ob_end_flush();
fpassthru($stream);
}, $name, [
'Content-Type' => $metaData['type'],
'Content-disposition' => 'attachment; filename="' . $name . '"',
]);
The solution could be saving to GCS with longer (random) .zip name as public for shorter period of time (for security reasons), and initiate download directly, the problem is that I haven't managed to do so without reaching memory limits:
$zip = new ZipArchive();
$zipName = time().'.zip';
$downloadFolderName = 'downloads';
$zipCreatedDirs = [];
if (! Storage::exists($downloadFolderName)) {
Storage::makeDirectory($downloadFolderName);
}
$zipPath = tempnam(sys_get_temp_dir(), time());
if (! $zip->open($zipPath, ZIPARCHIVE::OVERWRITE)) {
return response()->json([
'status' => 'error',
'message' => 'Failed to create .zip archive.'
]);
}
// Add files for download one by one to zip
// and split into different directories grouped by product
foreach($files as $file) {
if (! in_array($file['dirName'], $zipCreatedDirs)) {
$zip->addEmptyDir($file['dirName']);
$zipCreatedDirs[] = $file['dirName'];
}
$zip->addFromString($file['dirName'].'/'.$file['name'], Storage::get($file['src']));
}
$zip->close();
Storage::putFileAs($downloadFolderName, new File($zipPath), $zipName, 'private');
return response()->json([
'status' => 'success',
'downloadFiles' => [
[
'src' => route('download', [base64_encode($downloadFolderName.'/'.$zipName), base64_encode($zipName)]),
'name' => $zipName
]
]
], 200, [], JSON_NUMERIC_CHECK);
It seems that pretty much any of this doesn't work on GCE compared to Apache (local WAMP environment). If I set memory limit to 512 MB (the same as in GCE), I can download with the script above .zip of 1 GB without reaching memory limits.
Maybe I do something wrong, or don't understand something how it should be done. Any other thoughts or ideas? Any help would be appreciated.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.