Skip to content

Instantly share code, notes, and snippets.

@izoomi
Last active December 11, 2015 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izoomi/4559072 to your computer and use it in GitHub Desktop.
Save izoomi/4559072 to your computer and use it in GitHub Desktop.
Migrate from file system to s3 (paperclip)
# 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