Last active
December 11, 2015 06:28
-
-
Save izoomi/4559072 to your computer and use it in GitHub Desktop.
Migrate from file system to s3 (paperclip)
This file contains 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
# replace model with your model that has an attachment | |
# these definitions need to be defined on the model | |
definitions = Model.attachment_definitions | |
# root directory where you're storing your files | |
dir = '/home/railsapps/public_html/plenishable/shared' | |
Model.find_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)) | |
file = dir + attachment.url | |
file.gsub!(/\?.+/, '') | |
if File.exists?(file) | |
file_data = File.open(file) | |
puts "Saving file: #{dir + attachment.url} to Amazon S3..." | |
record.send("#{definition}").assign file_data | |
record.send("#{definition}").save | |
else | |
puts "Can't find file: #{dir + attachment.url} NOT MIGRATING..." | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment