-
-
Save t-anjan/9671291 to your computer and use it in GitHub Desktop.
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
# config/initializers/paperclip.rb | |
# If you want to disable paperclip attachments to S3 in the development environment, | |
# remove the comments from the "if" condition below and restart the server. | |
#if Rails.env.staging? || Rails.env.production? | |
Paperclip::Attachment.default_options[:storage] = :s3 | |
Paperclip::Attachment.default_options[:s3_credentials] = { | |
:bucket => "app-#{Rails.env}", | |
:access_key_id => '<ACCESS_KEY_ID>', | |
:secret_access_key => '<SECRET_ACCESS_KEY>' | |
} | |
Paperclip::Attachment.default_options[:url] = ':s3_domain_url' | |
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename' | |
# The only addition required to make the code work with DreamHost's DreamObjects.. | |
Paperclip::Attachment.default_options[:s3_host_name] = 'objects.dreamhost.com' | |
Paperclip::Attachment.default_options[:s3_protocol] = 'https' | |
#end |
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
# First setup the associated S3 config for paperclip (the attached initializer would do). | |
# Then add the classes your want to migrate in the klasses array below. | |
# Then run rake paperclip_migration:migrate_to_s3 | |
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. | |
klasses.each do |klass_key| | |
klass = klass_key.to_s.classify.constantize rescue nil | |
if klass.blank? | |
puts "#{klass_key.to_s.classify} is not defined in this app." | |
next | |
end | |
definitions = klass.respond_to?(:attachment_definitions) ? klass.attachment_definitions : nil | |
if definitions.blank? | |
puts "There are no paperclip attachments defined for the class #{klass.to_s}" | |
next | |
end | |
definitions.keys.each do |attachment| | |
records = klass.where("#{attachment}_file_name is not NULL") | |
records.each do |record| | |
path = record.send(attachment).path(:original) | |
full_path = File.join( Rails.root, 'public', 'system', path ) | |
unless File.exists?( full_path ) | |
puts "Can't find file: #{full_path} NOT MIGRATING..." | |
next | |
end | |
file_data = File.open( full_path ) | |
puts "Saving file: #{full_path} to Amazon S3..." | |
record.send("#{attachment}=", file_data) | |
record.save! | |
file_data.close | |
end | |
end | |
end | |
end | |
end |
Awesome, thanks for this. I think it is slightly out of date now that it's been over two years, it almost worked, but to get mine working with dream objects (latest version of paperclip, aws-sdk, and using rails 5) I had to set it up my initializer like this:
Paperclip::Attachment.default_options[:storage] = :s3
Paperclip::Attachment.default_options[:s3_credentials] = {
:bucket => ENV['AWS_BUCKET'],
:access_key_id => ENV['AWS_KEY'],
:secret_access_key => ENV['AWS_SECRET_KEY'],
s3_region: 'us-west-1'
}
Paperclip::Attachment.default_options[:s3_options] = {
endpoint: 'https://objects-us-west-1.dream.io'
}
Paperclip::Attachment.default_options[:url] = ':s3_domain_url'
Paperclip::Attachment.default_options[:path] = '/:class/:attachment/:id_partition/:style/:filename'
Paperclip::Attachment.default_options[:s3_host_name] = 'objects-us-west-1.dream.io'
Paperclip::Attachment.default_options[:s3_protocol] = 'https'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks, you made my morning