Guest User

Untitled

a guest
Jun 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. # First configure your models to use Amazon s3 as storage option and setup the associated S3 config.
  2. # Then add the classes your want to migrate in the klasses array below.
  3. # Then run rake paperclip_migration:migrate_to_s3
  4. # Should work but this is untested and may need some tweaking - but it did the job for me.
  5.  
  6. namespace :paperclip_migration do
  7. desc "migrate files from filesystem to s3"
  8. task :migrate_to_s3 => :environment do
  9. 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.
  10. klasses.each do |klass_key|
  11. if klass = real_klass(klass_key)
  12. if klass.respond_to?(:attachment_definitions) && definitions = klass.attachment_definitions
  13. klass.all.each do |record|
  14. definitions.keys.each do |definition|
  15. if record.send("#{definition}_file_name")
  16. attachment = Paperclip::Attachment.new(definition.to_sym, record, definitions[definition.to_sym].except(:s3_credentials, :storage))
  17. if File.exists?(Rails.root.to_s + "/public" + attachment.url)
  18. file_data = File.open(Rails.root.to_s + "/public" + attachment.url)
  19. puts "Saving file: #{Rails.root.to_s + "/public" + attachment.url} to Amazon S3..."
  20. record.send("#{definition}").assign file_data
  21. record.send("#{definition}").save
  22. else
  23. puts "Can't find file: #{Rails.root.to_s + "/public" + attachment.url} NOT MIGRATING..."
  24. end
  25. end
  26. end
  27. end
  28. else
  29. puts "There are not paperclip attachments defined for the class #{klass.to_s}"
  30. end
  31. else
  32. puts "#{key.to_s.classify} is not defined in this app."
  33. end
  34. end
  35. end
  36.  
  37. def real_klass(key)
  38. key.to_s.classify.constantize
  39. rescue
  40. end
  41. end
Add Comment
Please, Sign In to add comment