Skip to content

Instantly share code, notes, and snippets.

@leighmcculloch
Created November 7, 2015 17:31
Show Gist options
  • Save leighmcculloch/20c5caf59de63531ab79 to your computer and use it in GitHub Desktop.
Save leighmcculloch/20c5caf59de63531ab79 to your computer and use it in GitHub Desktop.
A simple tool that will help you delete files on buckets with MFA Delete feature enabled.
#!/usr/bin/env ruby
require 'aws-sdk'
print "AWS Account ID: "
aws_account_id = STDIN.gets.chomp
print "S3 bucket name: "
bucket_name = STDIN.gets.chomp
print "AWS Access Key ID: "
aws_id = STDIN.gets.chomp
print "AWS Access Secret Key: "
aws_key = STDIN.gets.chomp
s3_endpoint = "s3.amazonaws.com"
mfa_serial = "arn:aws:iam::#{aws_account_id}:mfa/root-account-mfa-device"
s3 = AWS::S3.new({
:s3_endpoint => s3_endpoint,
:access_key_id => aws_id,
:secret_access_key => aws_key
})
bucket = s3.buckets[bucket_name]
ARGV.each do |arg|
key, version = arg.split(":")
object = bucket.objects[key]
object_version = object.versions[version] if object
if object_version
print "AWS Root MFA Token (can be used once only): "
mfa_token = STDIN.gets.chomp
object_version.delete :mfa=>"#{mfa_serial} #{mfa_token}"
puts "Deleted: #{key}:#{version}"
else
puts "Not Found: #{key}:#{version}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment