Skip to content

Instantly share code, notes, and snippets.

@scottburton11
Last active January 11, 2023 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottburton11/0e1fd854d2d0f660b6fac1a776851e30 to your computer and use it in GitHub Desktop.
Save scottburton11/0e1fd854d2d0f660b6fac1a776851e30 to your computer and use it in GitHub Desktop.
Recover deleted S3 files from a versioned bucket
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])
@scottburton11
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment