Skip to content

Instantly share code, notes, and snippets.

@mltsy
Created April 2, 2013 17:37
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 mltsy/5294339 to your computer and use it in GitHub Desktop.
Save mltsy/5294339 to your computer and use it in GitHub Desktop.
# This is a quick script for doing a mass rename of all files in an Amazon S3 bucket.
# In this case, the rename operation was to unescape all filenames which had been
# previously escaped in error.
#############################
# Configuration:
bucketname = "YOUR_S3_BUCKET_NAME"
access_key = 'YOUR_ACCESS_KEY_ID'
secret_key = 'YOUR_SECRET_ACCESS_KEY'
#############################
require 'rubygems'
require 'aws/s3'
include AWS::S3
require 'cgi'
Base.establish_connection!(
:access_key_id => access_key,
:secret_access_key => secret_key
)
b = Bucket.find(bucketname)
marker = ''
while b.size > 0 do
puts "\n\n--------------------new page----------------------"
puts "\n From marker #{marker}"
puts "\n\n--------------------------------------------------"
b.each {|s3o|
if s3o.key.include? '%'
#TODO: check with a better regex - a simple "%" search includes escaped chars
#and legitimate files with % in the name
begin
S3Object.copy(CGI::escape(s3o.key), CGI::unescape(s3o.key), bucketname)
puts "copied #{CGI::escape(s3o.key)} to #{CGI::unescape(s3o.key)}"
#Uncomment this if you're feeling confident and want to delete the key
#s3o.delete
rescue Exception => e
puts "\n\n @@@@@@@@@@@@ EXCEPTION on key #{s3o.key} \n\n"
puts e.message
puts "@@@@@@@@@@@@@@}"
next
end
end
}
marker = b.objects.last.key
b = Bucket.find('presslift', {:marker => marker})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment