Skip to content

Instantly share code, notes, and snippets.

@markets
Created November 12, 2015 15:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save markets/8f972d5730a29672ba9b to your computer and use it in GitHub Desktop.
Save markets/8f972d5730a29672ba9b to your computer and use it in GitHub Desktop.
Migrate Paperclip assets from filesystem to AWS S3
namespace :attachments do
task migrate_to_s3: :environment do
require 'aws-sdk'
# Define Paperclip models
models = [
[Attachment, :data],
[Image, :data],
[MediaAppearance, :media_logo],
[Testimonial, :avatar_logo]
]
# Load credentials and config
s3_options = YAML.load_file(File.join(Rails.root, 'config/aws.yml'))[Rails.env].symbolize_keys
paperclip_options = YAML.load_file(File.join(Rails.root, 'config/paperclip.yml'))[Rails.env].symbolize_keys
bucket_name = paperclip_options[:bucket]
# Establish S3 connection
AWS.config(s3_options)
s3 = AWS::S3.new
bucket = s3.buckets[bucket_name]
models.each do |klass, field|
styles = []
klass.first.send(field).styles.each do |style|
styles.push(style[0])
end
styles.push(:original)
# Process
klass.find_each.with_index do |attachment, n|
styles.each do |style|
path = attachment.send(field).path(style)
next if path.nil?
begin
bucket.objects[path.sub!(%r{^/}, '')].write(file: File.join(Rails.public_path, path))
puts "-> Saved #{path} to S3 (#{n}/#{klass.count})"
rescue AWS::S3::Errors::NoSuchBucket => e
puts "--- Creating bucket named: #{bucket_name} ---"
AWS::S3.new.buckets.create(bucket_name)
retry
rescue StandardError => e
puts "Error on (#{n}/#{klass.count}): #{e}"
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment