Skip to content

Instantly share code, notes, and snippets.

@Dimimo
Last active February 15, 2022 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Dimimo/b9f6406988d6ce4151c77d0bb09bad1d to your computer and use it in GitHub Desktop.
Save Dimimo/b9f6406988d6ce4151c77d0bb09bad1d to your computer and use it in GitHub Desktop.
A Laravel artisan command to help move your files from S3 to Linode
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Storage;
class LinodeCopy extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'linode-copy
{folder=/ : The folder you want to copy, default to root}
{--D|directories : Include all subdirectories}
{--counter=0 : a counter where to start}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Copy files from S3 to Linode S3 object storage';
/**
* Define the s3 disk name of your filesystem
*
* @var string $s3
*/
protected $s3 = 's3';
/**
* Define the linode disk name of your filesystem
*
* @var string $linode
*/
protected $linode = 'linode';
/**
* The requested folder, defaults to '/' (root) if left empty
*
* @var string $folder
*/
protected $folder;
/**
* Request all underlying directories?
*
* @var bool $allDirectories
*/
protected $allDirectories;
/**
* The counter is a way to discard the files that has already been uploaded
*
* @var int $counter
*/
protected $counter = 0;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return void
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function handle()
{
$this->folder = $this->argument('folder');
$this->allDirectories = $this->option('directories');
$this->counter = $this->option('counter');
$files = $this->getList();
$count = count($files) - $this->counter;
if ($this->confirm('The total amount of files to copy is ' . $count . '. Continue?')) {
$this->copyFiles($files);
}
}
/**
* Get all files from the requested directory
* Optional, get all files including each underlying directory
*
* @return array
*/
protected function getList()
{
if ($this->allDirectories === true) {
return Storage::disk($this->s3)->allFiles($this->folder);
}
return Storage::disk($this->s3)->files($this->folder);
}
/**
* Copy all requested files in S3 directory to Linode
* Comment: defaults to public
*
* @param $files
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function copyFiles($files)
{
$this->info("Start copying of " . count($files) . " files....");
$this->newLine();
foreach ($files as $i => $file) {
if ($i > $this->counter && !Storage::disk($this->linode)->exists($file)) {
$this->output->write("<info>{$file}</info>");
$content = Storage::disk($this->s3)->get($file);
$this->output->write(' <comment> ...copying ' . $i . ' ...</comment> ');
$res = Storage::disk($this->linode)->put($file, $content, 'public');
unset($content);
if ($res) {
$this->output->write(' <info>success!</info>', true);
}
else {
$this->output->write(' <error>ERROR!</error>', true);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment