The Odd Developer
The Odd Developer

Reputation: 929

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.

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

Answers (1)

Arunas Bart
Arunas Bart

Reputation: 2688

  • I would check permissions as first possible issue, e.g. you run command manually on your user permission level, but web server probably runs on different user, typically much more restricted.
  • Second thing to check is the AWS access keys. I believe you have set up your working environment, but have you done so for http/web server user?

Upvotes: 1

Related Questions