Export excel file from Laravel Vapor through S3 bucket, code touch up

This commit is contained in:
Dillon Ngo
2024-06-18 10:34:04 +08:00
parent 700b72900a
commit 38aa670528
5 changed files with 53 additions and 63 deletions
@@ -8,7 +8,7 @@ use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Excel;
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;
use App\Classes\General\AWSS3Helper;
class ExportCustomersWalletTransactionToExcelController
{
@@ -29,7 +29,7 @@ class ExportCustomersWalletTransactionToExcelController
$exportFileName = $request->route('marking') . '-wallet-' . ($request->route('is_precise') == 'true' ? 'precise-' : '') . 'transaction-history.xls';
$filesystemDriver = Storage::getDefaultDriver();
if($filesystemDriver === 's3'){
return response([ 'src' => $this->S3($exportFileName, $exportsTransactions) ]);
return response([ 'src' => AWSS3Helper::S3($exportFileName, $exportsTransactions) ]);
}
else{
$response = $exportsTransactions->download($exportFileName, Excel::XLS, ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
@@ -37,18 +37,4 @@ class ExportCustomersWalletTransactionToExcelController
return $response;
}
}
private function S3($exportFileName, $exportableObject){
//Step 1: Upload to S3
$filePathForS3 = 'temp/' . $exportFileName;
$exportableObject->store($filePathForS3, 's3');
//Step 2: Return temporary URL from S3
$temporaryUrl = Storage::disk('s3')->temporaryUrl(
$filePathForS3,
Carbon::now()->addMinutes(10)
);
return $temporaryUrl;
}
}