Created
July 3, 2012 18:13
-
-
Save plainprogrammer/3041496 to your computer and use it in GitHub Desktop.
This is a quick script for doing a mass copy or rename of all files in an Amazon S3 bucket.
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
# 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 | |
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 =~ /\.m4a/i | |
begin | |
old_key = s3o.key | |
new_key = s3o.key.gsub(/\.m4a/i, '.mp4') | |
S3Object.copy(old_key, new_key, bucketname) | |
puts "copied #{old_key} to #{new_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