Skip to content

Instantly share code, notes, and snippets.

@wittawasw
Forked from louisgillies/paperclip_migration.rake
Last active August 29, 2015 13:56
Show Gist options
  • Save wittawasw/8950415 to your computer and use it in GitHub Desktop.
Save wittawasw/8950415 to your computer and use it in GitHub Desktop.
# First configure your models to use Amazon s3 as storage option and setup the associated S3 config.
# Then add the classes your want to migrate in the klasses array below.
# Then run rake paperclip_migration:migrate_to_s3
# Should work but this is untested and may need some tweaking - but it did the job for me.
namespace :paperclip_migration do
desc "migrate files from filesystem to s3"
task :migrate_to_s3 => :environment do
klasses = [:model_1, :model_2] # Replace with your real model names. If anyone wants to this could be picked up from args or from configuration.
klasses.each do |klass_key|
if klass = real_klass(klass_key)
if klass.respond_to?(:attachment_definitions) && definitions = klass.attachment_definitions
klass.all.each do |record|
definitions.keys.each do |definition|
if record.send("#{definition}_file_name")
attachment = Paperclip::Attachment.new(definition.to_sym, record, definitions[definition.to_sym].except(:s3_credentials, :storage))
attachment_url = Rails.root.to_s + "/" + attachment.path;
attachment_url.gsub!(/"/, '' )
if File.exists?("#{attachment_url}")
file_data = File.open("#{attachment_url}")
puts "Saving file: #{attachment_url} to Amazon S3..."
record.send("#{definition}").assign file_data
record.send("#{definition}").save
else
puts "Can't find file: #{attachment_url} NOT MIGRATING..."
end
end
end
end
else
puts "There are not paperclip attachments defined for the class #{klass.to_s}"
end
else
puts "#{key.to_s.classify} is not defined in this app."
end
end
end
def real_klass(key)
key.to_s.classify.constantize
rescue
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment