Skip to content

Instantly share code, notes, and snippets.

@igorb
Created September 4, 2013 09:06
Show Gist options
  • Save igorb/f6fcf8bf0cf43479afdb to your computer and use it in GitHub Desktop.
Save igorb/f6fcf8bf0cf43479afdb to your computer and use it in GitHub Desktop.
rake task to rename old S3 resources according to new URL encoding
# Maintenance script needed to rename old S3 resources according to new URL encoding
desc "One-time renaming of all the amazon s3 content"
task :rename_s3_files do |t, args|
require 'aws/s3'
require Rails.root.join('app', 'helpers', 'paperclip_sanitization_helper.rb')
cred = YAML.load(File.open("#{Rails.root}/config/s3.yml"))[Rails.env].symbolize_keys!
attr_bucket = cred.delete(:bucket)
AWS::S3::Base.establish_connection! cred
bucket = AWS::S3::Bucket.find(attr_bucket)
marker = ''
renamed = 0
fine = 0
failure = 0
page = 0
# Rename everything in the bucket
while bucket.size > 0 do
page += 1
puts "\n\n Page #{page}"
puts "\n From marker #{marker}"
puts "\n\n--------------------------------------------------"
bucket.each do |obj|
name = obj.key.to_s
encode_name = PaperclipSanitizationHelper.sanitize_string(name)
begin
if name == encode_name
fine += 1
else
if name.ascii_only?
obj.copy(encode_name)
else
obj.class.create(encode_name, obj.value, bucket.name)
end
renamed += 1
end
rescue Exception => e
failure += 1
puts "\n\n *****=***** EXCEPTION on key #{obj.key} \n\n"
puts e.message
puts "*****=*****"
next
end
end
marker = bucket.objects.last.key
bucket = AWS::S3::Bucket.find(attr_bucket, {:marker => marker})
end
puts "Renamed: #{renamed} / Failed: #{failure} / All: #{fine + renamed + failure} "
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment