Skip to content

Instantly share code, notes, and snippets.

@pedrorocha-net
Created February 17, 2022 20:00
Show Gist options
  • Save pedrorocha-net/cc538d8d4fd8463a6a0ea5acb6b3c2f0 to your computer and use it in GitHub Desktop.
Save pedrorocha-net/cc538d8d4fd8463a6a0ea5acb6b3c2f0 to your computer and use it in GitHub Desktop.
Laravel + Media Library code to migrate files from local filesystem to AWS S3
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Spatie\MediaLibrary\Models\Media;
class MigrateFiles extends Command
{
protected $signature = 'migrate-files';
public function handle()
{
$medias = Media::where('disk', 'public')->limit(5)->get();
$bar = $this->output->createProgressBar(count($medias));
$bar->start();
foreach ($medias as $Media) {
$Model = $Media->model;
$Media->move($Model, $Media->collection_name, 's3');
$bar->advance();
}
$bar->finish();
print "\n";
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment