From 4ec4ecbb7ae364f075d0fd81ed57bb790063a4d2 Mon Sep 17 00:00:00 2001 From: Dillon Ngo Date: Sat, 27 Apr 2024 10:36:56 +0800 Subject: [PATCH] Laravel Vapor - sync code for download files in bulk from S3 to more files --- .../V2/DeleteBulkInvoiceFilesV2CommandJob.php | 44 ++++++++++++++----- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/app/Classes/Jobs/Commands/V2/DeleteBulkInvoiceFilesV2CommandJob.php b/app/Classes/Jobs/Commands/V2/DeleteBulkInvoiceFilesV2CommandJob.php index 89733472..42c990ce 100644 --- a/app/Classes/Jobs/Commands/V2/DeleteBulkInvoiceFilesV2CommandJob.php +++ b/app/Classes/Jobs/Commands/V2/DeleteBulkInvoiceFilesV2CommandJob.php @@ -11,6 +11,7 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Storage; class DeleteBulkInvoiceFilesV2CommandJob implements ShouldQueue { @@ -23,25 +24,48 @@ class DeleteBulkInvoiceFilesV2CommandJob implements ShouldQueue $start = new Carbon(); $directories = [ - storage_path('app/bulk_invoice'), //cief todo: should map to the equivalent in AWS S3 bucket - storage_path('app/bulk_whiteform'), //cief todo: should map to the equivalent in AWS S3 bucket + storage_path('app/bulk_invoice'), + storage_path('app/bulk_whiteform'), ]; foreach ($directories as $directory) { - $start = new Carbon(); - Log::info(Carbon::now() . ' Start cleaning - ' . $directory); + $startInner = new Carbon(); + Log::info(Carbon::now() . ' [Local] Start cleaning - ' . $directory); - if (File::isDirectory($directory)) { //cief todo: should map to the equivalent in AWS S3 bucket + if (File::isDirectory($directory)) { File::cleanDirectory($directory); - Log::info('All files have been deleted.'); + Log::info('[Local] All files have been deleted.'); } else { - Log::info('Directory does not exist.'); + Log::info('[Local] Directory does not exist.'); } - $end = new Carbon(); - $elapsedTime = $start->diff($end)->format('%H:%I:%S'); + $endInner = new Carbon(); + $elapsedTime = $startInner->diff($endInner)->format('%H:%I:%S'); - Log::info(Carbon::now() . ' Process ended. ElapsedTime: ' . $elapsedTime); + Log::info(Carbon::now() . ' [Local] Process ended. ElapsedTime: ' . $elapsedTime); + } + + $s3 = Storage::disk('s3'); + $directories = [ + 'bulk_invoice', + 'bulk_whiteform', + ]; + foreach ($directories as $directory) { + $startInner = Carbon::now(); + Log::info(Carbon::now() . ' [S3] Start cleaning - ' . $directory); + + $objects = $s3->allFiles($directory); + + foreach ($objects as $object) { + $s3->delete($object); + Log::info('[S3] Deleted object: ' . $object); + } + + Log::info('[S3] All files have been deleted.'); + + $endInner = Carbon::now(); + $elapsedTime = $startInner->diff($endInner)->format('%H:%I:%S'); + Log::info(Carbon::now() . ' [S3] Process ended. ElapsedTime: ' . $elapsedTime); } $end = new Carbon();