Reputation: 929
In my laravel version 7 application, I have written a custom artisan command for accessing aws cli for pushing files to S3 from laravel.
Artisan command code:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class PushFiles extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'S3:push {file}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Push files';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$file = $this->argument('file');
exec('aws s3 cp '.$file.' s3://custom-bucket');
echo 'aws s3 cp '.$file.' s3://custom-bucket';
}
}
And in my controller I call this command like this:
$file_path = '/storage/app/'.$file_name;
$exit_code = \Artisan::call('S3:push',['file' => $file_path]);
return $exit_code;
Both $file_name and $file_path variables are assigned relevant values.
And this returns 0 as the exit code which means successful. But file is not copying to the S3 bucket.
File is copying to S3 bucket when executing the command in the terminal as below.
php artisan S3:push <Path to file which is same as $file_path>
Furthermore, I tried this code snippet as well to see the terminal output and it shows the exact handler code that I have echo.
dd(\Artisan::output());
This is really confusing me.
Upvotes: 0
Views: 564
Reputation: 2688
Upvotes: 1