Skip to content

Instantly share code, notes, and snippets.

@Bernix01
Created August 29, 2017 22:16
Show Gist options
  • Save Bernix01/82cf968114a7d4225a5254ea09b125fe to your computer and use it in GitHub Desktop.
Save Bernix01/82cf968114a7d4225a5254ea09b125fe to your computer and use it in GitHub Desktop.
Rename files in s3
# This is a modification of https://gist.github.com/voxxit/6c40a5583a2e3ff51dfa5725c069a830
# 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 = "the_incredible_bucket"
region = "region"
#############################
require "aws-sdk"
s3 = Aws::S3::Client.new(region: region)
objects = []
start_after = nil
loop do
resp = s3.list_objects_v2(
bucket: bucketname,
max_keys: ENV.fetch("max_keys", 1000).to_i,
start_after: start_after,
prefix: ENV["prefix"]
)
objects += resp.contents
start_after = resp.contents.last.key
break if resp.is_truncated != true
end
objects.each do |object|
if object.key =~ /funnyregex/
print(object.key)
print("\n")
nkey = 'renamed_' + object.key
#make a new object
s3.copy_object(
bucket: bucketname,
copy_source: bucketname + "/" + object.key,
key: nkey
)
#delete old object
s3.delete_object(
bucket: bucketname,
key: object.key
)
print("################################\n")
print("\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment