Laravel-Excel: [Bug]: Creating exports on Laravel Vapor: Could not find zip member zip

Is the bug applicable and reproducable to the latest version of the package and hasn’t it been reported before?

  • Yes, it’s still reproducable

What version of Laravel Excel are you using?

3.1.55

What version of Laravel are you using?

11.19.0

What version of PHP are you using?

8.3

Describe your issue

When doing exports within a Vapor environment, it seems like the AppendQueryToSheet job is spawned before the file is fully created on S3.

Keep running into: PhpOffice\PhpSpreadsheet\Reader\Exception Could not find zip member zip:///tmp/laravel-excel-BSReujKfyopUuGIeaQy50PP4DyuxvXee.xlsx#_rels.rels

When viewing S3 the file does exist. Additionally, sometimes the export works, but most of the times it fails. Vapor (AWS) doesn’t allow you to create a queue of size 1, so the other chunks of jobs are usually picked up by another worker.

    (new EventFinancialsExport($this->event))->queue($path, 'exports')->allOnQueue('queue-exports')->chain(
        [
            new SendExportCompletedNotification($user, $path),
        ]
    );

Config:

'temporary_files' => [
    'remote_disk' => 'tmp',
];

FileSystem:

'tmp' => [
    'driver' => 's3',
    'key' => env('AWS_ACCESS_KEY_ID'),
    'secret' => env('AWS_SECRET_ACCESS_KEY'),
    'region' => env('AWS_DEFAULT_REGION'),
    'bucket' => env('AWS_BUCKET'),
    'url' => env('AWS_URL'),
    'directory_separator' => '/',
    'root' => 'tmp/',
],

How can the issue be reproduced?

This isn’t easy to recreate unfortunately.

You need to be on Laravel Vapor. Create an environment with queued workers. Create an export with a chained mail or reporting job on a specific queue. You need to export items that has a large number of rows, forcing chucking of query results. The jobs being spawned will grab the file before it is ready creating the error.

What should be the expected behaviour?

I think a solution could be just to wrap the AppendQueryToSheet within a quick retry function or anywhere where the file is being accessed if jobs were spawned, just to try and get the file again creating a small delay. I believe another job is fired too fast off the queue and is accessing the file while it is being created on S3 in an incomplete state.

This is how I solved the same issue (error was the same) in another line of code where we would download the file directly if small enough.

// This fixed the same issue as described above
$export = (new EventFinancialsExport($this->event));

return retry(5, function () use ($export, $path) {
    return $export->download($path);
}, 100, function ($exception) {
    $this->emitNotification('An error occurred while generating the report. Please try again or contact support.');
});

I could be entirely off the mark also, any feedback or suggestions would be greatly appreciated.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 23 (13 by maintainers)

Most upvoted comments

Can confirm the same issue. Been struggling with this since export size got bigger than 1 request could handle.

After upgrading 3.1.55 => 3.1.58 it feels that the problem has actually gotten worse - even less chance of successful export.

Serverless Vapor. Without Octane.

I actually tried to work around this using failed() method, but it never gets called. #4078 Now I understand that the issue lies elsewhere.

EDIT: isRunningServerless() correctly skips the

if ($temporaryFile instanceof RemoteTemporaryFile && !$temporaryFile->existsLocally() && !$this->isRunningServerless()) {
$temporaryFile = resolve(TemporaryFileFactory::class)
->makeLocal(Arr::last(explode('/', $temporaryFile->getLocalPath())));
}

But fails on this:

$writer->save(
$temporaryFile->getLocalPath()
);

Relevant stacktrace

ErrorException:
fopen(/tmp/storage/framework/cache/laravel-excel/laravel-excel-I8nSadaIegJiuyoBXrOOjLTwomglGcL2.xlsx): Failed to open stream: No such file or directory

  at /var/task/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/BaseWriter.php:128
  at Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'fopen(/tmp/storage/framework/cache/laravel-excel/laravel-excel-I8nSadaIegJiuyoBXrOOjLTwomglGcL2.xlsx): Failed to open stream: No such file or directory', '/var/task/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/BaseWriter.php', 128)
     (/var/task/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:255)
  at Illuminate\Foundation\Bootstrap\HandleExceptions->Illuminate\Foundation\Bootstrap\{closure}(2, 'fopen(/tmp/storage/framework/cache/laravel-excel/laravel-excel-I8nSadaIegJiuyoBXrOOjLTwomglGcL2.xlsx): Failed to open stream: No such file or directory', '/var/task/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/BaseWriter.php', 128)
  at fopen('/tmp/storage/framework/cache/laravel-excel/laravel-excel-I8nSadaIegJiuyoBXrOOjLTwomglGcL2.xlsx', 'wb')
     (/var/task/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/BaseWriter.php:128)
  at PhpOffice\PhpSpreadsheet\Writer\BaseWriter->openFileHandle('/tmp/storage/framework/cache/laravel-excel/laravel-excel-I8nSadaIegJiuyoBXrOOjLTwomglGcL2.xlsx')
     (/var/task/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx.php:551)
  at PhpOffice\PhpSpreadsheet\Writer\Xlsx->save('/tmp/storage/framework/cache/laravel-excel/laravel-excel-I8nSadaIegJiuyoBXrOOjLTwomglGcL2.xlsx')
     (/var/task/vendor/maatwebsite/excel/src/Writer.php:184)
  at Maatwebsite\Excel\Writer->write(object(OrderItemExport), object(RemoteTemporaryFile), 'Xlsx')
     (/var/task/vendor/maatwebsite/excel/src/Jobs/QueueExport.php:81)
  at Maatwebsite\Excel\Jobs\QueueExport->Maatwebsite\Excel\Jobs\{closure}(object(QueueExport))
     (/var/task/vendor/maatwebsite/excel/src/Jobs/Middleware/LocalizeJob.php:46)
  at Maatwebsite\Excel\Jobs\Middleware\LocalizeJob->Maatwebsite\Excel\Jobs\Middleware\{closure}()
     (/var/task/vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php:19)
  at Maatwebsite\Excel\Jobs\Middleware\LocalizeJob->withLocale(null, object(Closure))
     (/var/task/vendor/maatwebsite/excel/src/Jobs/Middleware/LocalizeJob.php:45)
  at Maatwebsite\Excel\Jobs\Middleware\LocalizeJob->handle(object(QueueExport), object(Closure))
     (/var/task/vendor/maatwebsite/excel/src/Jobs/QueueExport.php:62)

The PR adresses only runs on AWS as our issues is on lambda functions. For kubernetes, you’d have to find a way to understand how you can see it runs in K8, maybe an env variable again. Did you try commenting the lines altogether to see if it fixes your issue ? (I guess it will as you are probably ending on another container, assuming your temp storage exists, is writable etc) If you do find a way and are up to PR it, maybe the method I created should be renamed to reflect your change.

@devinfd I am not using Octane. We use S3, and we have filled both settings for temporary files. Take a look

'local_path'          =>  env('APP_ENV') === 'local'
            ? storage_path('framework/cache/laravel-excel')
            : sys_get_temp_dir(),
'remote_disk'         => 's3-tenant', // Multi-tenancy,
'force_resync_remote' => true,

Ok, Octane is my first guess to the source of the problem. I suspect this is not a Laravel Excel problem but I’m not certain yet. I’m going to spend some time on this bug over the next few days.