Skip to content

Instantly share code, notes, and snippets.

@elenakondrateva
Last active March 8, 2020 20:12
Show Gist options
  • Save elenakondrateva/ffad4438ea607d9baff82f305c332056 to your computer and use it in GitHub Desktop.
Save elenakondrateva/ffad4438ea607d9baff82f305c332056 to your computer and use it in GitHub Desktop.
Synchronise OctoberCMS media files with AWS S3 cloud
<?php
namespace Acme\Console;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Storage;
class OneOffSyncUploads extends Command
{
private $folders = [
'uploads',
'media',
];
/**
* @var string The console command name.
*/
protected $name = 'acme:one-off:sync-uploads';
/**
* @var string The console command description.
*/
protected $description = 'Put existing files from the disk to the AWS S3 cloud';
/**
* Create a new command instance.
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
* @return void
*/
public function fire()
{
$disk = Storage::disk('local');
$cloud = Storage::disk('s3');
foreach($this->folders as $folder) {
$this->info(sprintf('Syncing folder %s...', $folder));
if(!$disk->exists($folder)) {
throw new \Exception(sprintf('No such directory %s', $folder));
}
$files = $disk->allFiles($folder);
if ($filesCount = count($files)) {
$this->info(sprintf('Found %d files.', $filesCount));
$bar = $this->output->createProgressBar($filesCount);
$existingCount = 0;
$writtenCount = 0;
foreach ($files as $file) {
$bar->advance();
if ($cloud->has($file)) {
$existingCount++;
continue;
}
if($cloud->put($file, $disk->get($file))) {
$writtenCount++;
}
}
$bar->finish();
}
$this->output->writeln('');
$this->info(sprintf('Folder %s is synced: %d files exist, %d files written', $folder, $existingCount, $writtenCount));
}
$this->info('All done! You can remove files from disk now.');
}
/**
* Get the console command arguments.
* @return array
*/
protected function getArguments()
{
return [
// ['example', InputArgument::REQUIRED, 'An example argument.'],
];
}
/**
* Get the console command options.
* @return array
*/
protected function getOptions()
{
return [
// ['example', null, InputOption::VALUE_OPTIONAL, 'An example option.', null],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment