Skip to content

Instantly share code, notes, and snippets.

@Diablo2050
Created February 17, 2021 07:49
Show Gist options
  • Save Diablo2050/32f74de0a15d6a1d6b6daad97a09ef55 to your computer and use it in GitHub Desktop.
Save Diablo2050/32f74de0a15d6a1d6b6daad97a09ef55 to your computer and use it in GitHub Desktop.
S3 recover deleted folders with versioning enabled.
#!/bin/bash
# if you have profiles other than default unhash the below line and profide the profile name
#export AWS_PROFILE=NAME
recoverfiles=$(aws s3api list-object-versions --bucket BUCKET_NAME --prefix FOLDER_NAME/ --query "DeleteMarkers[?IsLatest && starts_with(LastModified,'2021-02-16')].{Key:Key,VersionId:VersionId}")
for row in $(echo "${recoverfiles}" | jq -c '.[]'); do
key=$(echo "${row}" | jq -r '.Key' )
# versionId=$(echo "${row}" | jq -r '.VersionId' )
aws s3api delete-object --bucket inno-themes-prod --key $key --version-id null
done