Skip to content

Instantly share code, notes, and snippets.

@lassebunk
Last active September 14, 2022 07:18
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lassebunk/cce8b54d7d36da0960eb to your computer and use it in GitHub Desktop.
Save lassebunk/cce8b54d7d36da0960eb to your computer and use it in GitHub Desktop.
Migrate all Dragonfly images to Amazon S3 using rake task
# This will move all images and other Dragonfly assets from your local server file system to Amazon S3.
namespace :dragonfly do
task :migrate_to_s3 => :environment do
# Adjust this line to meet your needs:
{ Product => [:image_uid, :other_uid], Page => :image_uid }.each do |klass, col|
puts "Migrating #{klass.table_name}..."
Array(col).each do |col|
klass.where("#{col} != ''").find_each do |instance|
begin
old_uid = instance.send(col)
puts "Migrating #{old_uid}..."
old_path = Rails.root.join("public/system/dragonfly", Rails.env, old_uid)
if File.exist?(old_path)
new_uid = Dragonfly.app.store old_path
instance.update_attributes col => new_uid
FileUtils.rm_f old_path
FileUtils.rm_f "#{old_path}.meta.yml"
puts "Done"
else
puts "Already migrated"
end
rescue => e
puts "Failed: #{e.message}"
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment