I trying to migrate my files from Tigris to Cloudflare R2 but whenever I run my migration task it fails…
Below is my task
# frozen_string_literal: true
namespace :active_storage do
desc "Migrate blobs from Tigris to R2"
task migrate_to_r2: :environment do
puts "Starting migration from Tigris to R2..."
r2_config = Rails.configuration.active_storage.service_configurations[:r2]
r2_service = ActiveStorage::Service.configure(:r2, Rails.configuration.active_storage.service_configurations)
puts "r2_service class: #{r2_service.class}"
puts "Upload method available? #{r2_service.respond_to?(:upload)}"
total_blobs = ActiveStorage::Blob.where(service_name: 'tigris').count
migrated_count = 0
failed_count = 0
puts "Found #{total_blobs} blobs to migrate (including variants)"
ActiveStorage::Blob.where(service_name: 'tigris').find_each do |blob|
begin
if blob.service.exist?(blob.key)
file_data = blob.download
r2_service.upload(blob.key, StringIO.new(file_data))
blob.update!(service_name: 'r2')
migrated_count += 1
puts "✓ Migrated blob #{blob.id} (#{blob.filename}) - #{migrated_count}/#{total_blobs}"
else
raise ActiveStorage::FileNotFoundError, "File not found in Tigris: #{blob.key}"
end
rescue ActiveStorage::FileNotFoundError => e
failed_count += 1
puts "✗ Failed to migrate blob #{blob.id} (#{blob.filename}): #{e.message}"
rescue => e
failed_count += 1
puts "✗ Failed to migrate blob #{blob.id} (#{blob.filename}): #{e.message}"
end
sleep(0.1)
end
puts "\nMigration completed!"
puts "Successfully migrated: #{migrated_count}"
puts "Failed: #{failed_count}"
puts "Total processed: #{migrated_count + failed_count}"
end
end
When I run it as follows
fly ssh console -C "bin/rake active_storage:migrate_to_r2 --trace"
Connecting to fdaa:2:f8b7:a7b:136:4a0b:c00e:2... complete
** Invoke active_storage:migrate_to_r2 (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute active_storage:migrate_to_r2
rake aborted!
NoMethodError: undefined method `[]' for nil (NoMethodError)
r2_config = Rails.configuration.active_storage.service_configurations[:r2]
^^^^^
/rails/lib/tasks/migrate_from_tigris_to_R2.rake:8:in `block (2 levels) in <main>'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `block in execute'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `each'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:281:in `execute'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:219:in `block in invoke_with_call_chain'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `synchronize'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:199:in `invoke_with_call_chain'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/task.rb:188:in `invoke'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:188:in `invoke_task'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `block (2 levels) in top_level'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `each'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:138:in `block in top_level'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:147:in `run_with_threads'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:132:in `top_level'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:83:in `block in run'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:214:in `standard_exception_handling'
/usr/local/bundle/ruby/3.3.0/gems/rake-13.2.1/lib/rake/application.rb:80:in `run'
bin/rake:5:in `<main>'
Tasks: TOP => active_storage:migrate_to_r2
Starting migration from Tigris to R2...
Error: ssh shell: Process exited with status 1
I think it is not loading the environment properly. My file upload works properly with R2 is just the migration script that is failing. This is a one time operation.