Spatie/media-library is is possible to move file to another disk?
I'm migrating files from one cloud service to another - is there an easy way to copy the files and update the DB?
I don't want to create new Db records but update the existing ones.
Spaties "extensive" documentation is quite lacking in some areas (Love your work Spatie, Honest)
I would suggest using the copy method first as it leaves the original files intact, then once your operation is complete, delete any DB media records that have the old disk name.
To anyone who comes here later (as I did because it's the top result in Google). Here's a command I built to migrate. All it does it check the disk of the media items, and move any that aren't in the correct disk.
Note that it uses all(), which if you have a LOT should be changed. Just did this as I set up a new site, forgot to change disk from local to s3, and only had like 5 images uploaded.
<?php
declare(strict_types=1);
namespace App\Console\Commands\Media;
use Illuminate\Console\Command;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
final class MigrateFromOldDiskCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'media:migrate';
/**
* The console command description.
*
* @var string
*/
protected $description = 'This command will migrate all media from the old disk to the new disk.';
/**
* Execute the console command.
*/
public function handle()
{
$media = Media::all();
$disk = config('media-library.disk_name');
foreach ($media as $item) {
$currentDisk = $item->disk;
$this->info('Processing media '.$item->id.', current disk: '.$currentDisk);
if ($currentDisk !== $disk) {
$item->move($item->model, $item->collection_name, $disk);
$this->info("Media {$item->id} has been copied to the new disk.");
} else {
$this->info("Media {$item->id} is already on the new disk.");
}
}
}
}