0

I have the following code to create a zip archive in Laravel 9 to an s3 disk using Flysystem but no file is being generated and nothing in the error logs. I have the following packages installed:

"installed:"league/flysystem-aws-s3-v3": "^3.0",
"league/flysystem-ziparchive": "^3.12",

Below is the full code.

Please note I refactored this line $filesystem = new Filesystem(new ZipArchiveAdapter(new FilesystemZipArchiveProvider($fileName))); using hack provided on https://github.com/thephpleague/flysystem-ziparchive/issues/17 as previously it was throwing error similar to what is described in that issue.

use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
use League\Flysystem\ZipArchive\ZipArchiveAdapter;
use League\Flysystem\ZipArchive\FilesystemZipArchiveProvider;

class ProcessBulkDownload implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $tries = 3;

    protected $bulkDownload;


    public function __construct(BulkDownload $bulkDownload)
    {
        $this->bulkDownload = $bulkDownload;
    }

    public function handle(ProfileRepositoryInterface $profileRepository)
    {
       $profiles = $profileRepository->profiles()->get();

       // Zip File Name
       $fileName = Str::random(40) . '.zip';

       if (config('filesystems.default') === 's3') {
            $adapter = new AwsS3V3Adapter(Storage::disk('s3')->getClient(), config('filesystems.disks.s3.bucket'), 'bulkdownload');
       } else {
            $adapter = new Local(storage_path('app/bulkdownload'));
       }

       $filesystem = new Filesystem(new ZipArchiveAdapter(new FilesystemZipArchiveProvider($fileName)));

       // Add files to the zip archive
       foreach ($profiles as $profile) {
           if ($profile->profile_img) {
            
               $filePath =  Str::kebab($profile->full_name) . '-' . $profile->id . '.' . pathinfo($profile->profile_img, PATHINFO_EXTENSION);
                   
               if (config('filesystems.default') === 's3') {
                   $filesystem->write(
                       $filePath,
                       Storage::disk('s3')->get($profile->profile_img)
                   );
               } else {
                   $filesystem->write(
                       $filePath,
                       file_get_contents(storage_path('app/' . $profile->profile_img))
                   );
               }
           }
       }

       // If the archive was created successfully
       if ($filesystem->has($fileName)) {
           $newFile = \App\File::create([
               "name" => $this->bulkDownload->localize('created_at')->format('Y-m-d-H-i-s') . '.zip',
               "path" => 'bulkdownload/' . $fileName,
               "size" => $filesystem->getSize($fileName),
               "mime_type" => $filesystem->getMimetype($fileName),
               "file_type" => 'BulkDownload',
               "fileable_id" => $this->bulkDownload->id,
               "fileable_type" => 'App\BulkDownload',
           ]);

           $this->bulkDownload->status_id = 16; // created
           $this->bulkDownload->save();
       } else {
           $this->bulkDownload->status_id = 17; // cancelled
           $this->bulkDownload->save();
       }
   }
} 

What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
adam78
  • 9,668
  • 24
  • 96
  • 207

0 Answers0