Last active
September 14, 2022 07:18
-
-
Save lassebunk/cce8b54d7d36da0960eb to your computer and use it in GitHub Desktop.
Migrate all Dragonfly images to Amazon S3 using rake task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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