Created
February 10, 2012 17:10
-
-
Save raphaelcm/1790999 to your computer and use it in GitHub Desktop.
Migrate local Refinery images/resources to S3
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
#!/usr/bin/env ruby | |
# Migrate local Refinery images & resources to S3. | |
# Assumes you've set up an AWS::S3 connection | |
# elsewhere (I have an initializer that does it). | |
image_classes = [Image] | |
data_classes = [Resource] | |
def upload(path, file, bucket, retries = 3) | |
begin | |
AWS::S3::S3Object.store(path, file, bucket, :access => :public_read) | |
sleep 1 | |
rescue => e | |
put "Upload failed: #{e.message}" | |
if retries > 0 | |
puts "Retrying..." | |
return upload(path, file, bucket, (retries - 1)) | |
else | |
puts "FAILED: #{path}#{file} to #{bucket}" | |
return false | |
end | |
end | |
true | |
end | |
def migrate_to_s3(class_name, type, directory, bucket) | |
puts "Migrating #{class_name} (#{type}) to S3..." | |
styles = [] | |
# Process each attachment | |
class_name.all.each_with_index do |attachment, n| | |
success = false | |
path = attachment.send(type.to_s).path rescue nil | |
file = attachment.send(type.to_s).file rescue nil | |
if path && file | |
path.gsub!(/#{Rails.root.to_s + '/public/system/' + directory}/, '') | |
success = upload(path, file, bucket) | |
puts "Saved #{path} to S3 (#{n}/#{class_name.count})" | |
end | |
end | |
end | |
# Get refinery bucket name | |
b = "some_s3_bucket" | |
image_classes.each do |class_name| | |
migrate_to_s3(class_name, :image, "images", b) | |
end | |
data_classes.each do |class_name| | |
migrate_to_s3(class_name, :file, "resources", b) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment