@codecanyon I’m really confused as to why you’re trying to execute console commands in a HTTP controller, or why you think you can’t use the AWS SDK with a role. You absolutely can.
Laravel custom artisan command works in terminal with php artisan, but not in code although exit code is 0
In my laravel version 7 application, I have written a custom artisan command for accessing aws cli for pushing files to S3 from laravel.
I'm using AWS CLI S3 commands directly because I'm not using AWS credentials. My app is hosted on a AWS EC2 instance and IAM role has been attached to it already with full permissions to access relevant S3 bucket programatically.
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.
Please or to participate in this conversation.