laraveldownloadzipminio

Download folder (or list of file) with large file (500MB) and build zip with laravel and minio


I'm trying to download a folder by calling my controller in Laravel. However, I'm running into a "memory exceeded" issue because one of the files I'm downloading is around 500MB.

Currently, I'm downloading each file from my MinIO storage and then zipping them. But this doesn't work in all cases due to memory constraints.

Does anyone have a solution to either:

Download a zipped folder directly from MinIO (I couldn't find this in the documentation), or

Stream the files from MinIO directly into the zip archive to reduce memory usage?

Here's my current code, but I'm open to completely changing the approach if needed.

private function getDescendents(
    FileOrFolder $folder,
    ZipArchive &$zip,
    string $path
) : void {
    foreach ($folder->childrens as $children) {
        $this->addDescendent($children, $zip, $path);
    }
}

private function addDescendent(
    FileOrFolder $fileSystemItem,
    ZipArchive $zip,
    string $path
) : void {

    $this->pushInZip($fileSystemItem, $zip, $path);

    if($fileSystemItem->type == FileOrFolderType::Folder) {
        $path .= $fileSystemItem->name . '/';
        $this->getDescendents($fileSystemItem, $zip, $path);
    }
}

private function pushInZip(
    FileOrFolder $fileSystemItem,
    ZipArchive $zip,
    string $path
) : void {

    $minioStorage = Storage::disk('minio');

    if($fileSystemItem->type == FileOrFolderType::Folder) {
        $zip->addEmptyDir($path . $fileSystemItem->name);
        return;
    }

    $fullPath = PathBuilder::buildFullPath($fileSystemItem);
    $fileStream = $minioStorage->readStream($fullPath);
    $filePathForZip = $path . $fileSystemItem->name;

    $zip->addFromString($filePathForZip, stream_get_contents($fileStream));
}

Solution

  • I use Laravel-ZipStream, it resolve all my problems !
    Thanks !