· 7 years ago · Nov 21, 2018, 02:08 AM
1# This is a quick script for doing a mass rename of all files in an Amazon S3 bucket.
2# In this case, the rename operation was to unescape all filenames which had been
3# previously escaped in error.
4
5#############################
6# Configuration:
7
8bucketname = "YOUR_S3_BUCKET_NAME"
9access_key = 'YOUR_ACCESS_KEY_ID'
10secret_key = 'YOUR_SECRET_ACCESS_KEY'
11
12#############################
13
14require 'rubygems'
15require 'aws/s3'
16include AWS::S3
17
18Base.establish_connection!(
19 :access_key_id => access_key,
20 :secret_access_key => secret_key
21 )
22
23b = Bucket.find(bucketname)
24marker = ''
25while b.size > 0 do
26 puts "\n\n--------------------new page----------------------"
27 puts "\n From marker #{marker}"
28 puts "\n\n--------------------------------------------------"
29 b.each {|s3o|
30 if s3o.key =~ /\.m4a/i
31 begin
32 old_key = s3o.key
33 new_key = s3o.key.gsub(/\.m4a/i, '.mp4')
34
35 S3Object.copy(old_key, new_key, bucketname)
36 puts "copied #{old_key} to #{new_key}"
37
38 #Uncomment this if you're feeling confident and want to delete the key
39 #s3o.delete
40 rescue Exception => e
41 puts "\n\n @@@@@@@@@@@@ EXCEPTION on key #{s3o.key} \n\n"
42 puts e.message
43 puts "@@@@@@@@@@@@@@}"
44 next
45 end
46
47 end
48 }
49 marker = b.objects.last.key
50 b = Bucket.find('presslift', {:marker => marker})
51end