ruby-on-railsrails-activestorage

Want to change my config.active_storage.service but keep or migrate my existing files


So I didn't realize that I had been saving files and images in production to config.active_storage.service = :local

I want it to match my staging which is config.active_storage.service = :digitalocean_spaces

The problem is when I switch it I break all the links to the old images. Question is how do I migrate those files into my digitalocean_spaces or even get them out of active_storage.serivce local to upload them into my digital oceans space


Solution

  • You can now use Mirror Service:

    You can keep multiple services in sync by defining a mirror service. A mirror service replicates uploads and deletes across two or more subordinate services.

    A mirror service is intended to be used temporarily during a migration between services in production. You can start mirroring to a new service, copy pre-existing files from the old service to the new, then go all-in on the new service.

    You'll also need a custom script to sync your files between the mirrors. Basically, you have to call mirror_later on each blob (works on Rails 6.1+):

    ActiveStorage::Blob.all.each do |blob|
      blob.mirror_later
    end
    

    You can read more examples and approaches in the following thread: How to sync new ActiveStorage mirrors?