From 3e50431b9f187537ab4dcbaa46f670be76e03788 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Mon, 17 Jun 2024 11:57:23 +0800 Subject: [PATCH] Export file directly from Laravel Vapor without going through S3 bucket --- .../ExportCustomersToExcelController.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/Exports/ExportCustomersToExcelController.php b/app/Http/Controllers/Exports/ExportCustomersToExcelController.php index e3a1926c..69a70ae7 100644 --- a/app/Http/Controllers/Exports/ExportCustomersToExcelController.php +++ b/app/Http/Controllers/Exports/ExportCustomersToExcelController.php @@ -61,15 +61,22 @@ class ExportCustomersToExcelController $exportFileName = 'payment-transactions.xls'; $filesystemDriver = Storage::getDefaultDriver(); if($filesystemDriver === 's3'){ - $exportDirectory = storage_path('app/export'); // Update this with the actual directory path + + //Step 1: Upload to S3 + $filePathForS3 = 'temp/' . $exportFileName; + $exportsTransactions->store($filePathForS3, 's3'); + + //Step 2: Read and Download from S3 + $fileContent = Storage::disk('s3')->get($filePathForS3); + $exportDirectory = storage_path('app/temp'); // Update this with the actual directory path if (!file_exists($exportDirectory)) { mkdir($exportDirectory, 0755, true); } - // $localPath = $exportDirectory . '/' . $exportFileName; - $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); + $localPath = $exportDirectory . '/' . $exportFileName; + file_put_contents($localPath, $fileContent); - // file_put_contents($localPath, $response); - return response()->file($response->getFile()->getPathname(), ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); + //Step 3: Return + return response()->file($localPath, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']); } else{ $response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);