Last active
January 11, 2023 23:22
-
-
Save scottburton11/0e1fd854d2d0f660b6fac1a776851e30 to your computer and use it in GitHub Desktop.
Recover deleted S3 files from a versioned bucket
This file contains hidden or 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
require 'json' | |
def recover_bucket(bucket) | |
list = `aws s3api list-object-versions --bucket #{bucket} --output json --query 'DeleteMarkers[?IsLatest==\`true\`].[Key, VersionId]'` | |
pairs = JSON.parse(list) | |
pairs.each do |(key, versionId)| | |
STDOUT.puts `aws s3api delete-object --bucket #{bucket} --key #{key} --version-id #{versionId}}` | |
end | |
end | |
recover_bucket(ARGV[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I know
JSON.parse(list)
is memory-inefficient but if you are undeleting millions of files you don't have time to argue with me about it.