Skip to content

Instantly share code, notes, and snippets.

@princealikhan
Created April 9, 2017 18:57
Show Gist options
  • Save princealikhan/71fd2aeeed3255a94f18755e566b8363 to your computer and use it in GitHub Desktop.
Save princealikhan/71fd2aeeed3255a94f18755e566b8363 to your computer and use it in GitHub Desktop.
Push Elixir Build Assets To AWS S3 Bucket (CSS,JS)
<?php namespace App\Console\Commands\Build;
use Illuminate\Console\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Aws\S3\Exception\S3Exception;
use Storage;
class AssetBuildCommand extends Command {
/**
* The console command name.
*
* @var string
*/
protected $name = 'push:asset:build';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Push All the assets to AWS CDN';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function fire()
{
$this->info('<fg=green>Getting local build files</fg=green>');
/**
* New Build Assets
*/
$manifest = json_decode(file_get_contents(public_path('build/rev-manifest.json')), true);
$this->info('<fg=yellow>Upload in progress......</fg=yellow>');
foreach ($manifest as $key => $value)
{
$buildPath = 'build/'.$value;
if(Storage::exists($buildPath) == FALSE )
{
$this->info('<fg=cyan> Uploading -- '.$value.'</fg=cyan>');
try {
Storage::disk('s3')->getDriver()->put($buildPath,file_get_contents(public_path('build/'.$value)),[
'visibility' => 'public',
'Expires' => gmdate('D, d M Y H:i:s T', strtotime('+5 years')),
'CacheControl' => 'max-age=2628000',
]);
} catch (S3Exception $e) {
$this->console->writeln('<fg=red>'.$e->getMessage().'</fg=red>');
return false;
}
$this->info('<fg=green>Upload completed successfully.</fg=green>');
}else{
$this->info('<fg=yellow> Already uploaded -- '.$value.'</fg=yellow>');
}
}
}
}
php artisan push:asset:build
Add Command in Kernel
protected $commands = [
'App\Console\Commands\Build\AssetBuildCommand',
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment