To migrate documents stored in ActiveStorage from S3 to a storage in the local file system, do the following:
-
Configure a
local
service inconfig/storage.yml
:local: service: Disk root: <%= Rails.root.join("storage") %>
-
Run the following script to download all files from S3 and store them in your local file system. Note that at while this runs, you should not create new documents as those would otherwise be missing.
def migrate(from, to) configs = Rails.configuration.active_storage.service_configurations from_service = ActiveStorage::Service.configure from, configs to_service = ActiveStorage::Service.configure to, configs ActiveStorage::Blob.service = from_service puts "#{ActiveStorage::Blob.count} Blobs to go..." ActiveStorage::Blob.find_each do |blob| print '.' blob.open do |tf| checksum = blob.checksum to_service.upload(blob.key, tf, checksum: checksum) end end end migrate(:amazon, :local)
Script taken from https://www.stefanwienert.de/blog/2018/11/05/active-storage-migrate-between-providers-from-local-to-amazon/
-
Change the storage service in
config/production.rb
tolocal
and re-deploy:config.active_storage.service = :local
-
You can now re-enable creation of new documents.