Skip to content
Okko.eu
Go back

Restore S3 versioned files after accidental delete

Using jq proved handy when parsing Amazon AWS s3api JSON responses.

jq manual
jq cookbook
jq FAQ

The magic command:

aws s3api list-object-versions —bucket BUCKETNAME —prefix some/path/in/bucket/ | jq —raw-output ’.Versions | sort_by(.LastModified) | .[] | select(.LastModified < “2015-12-08T00:00:00.000Z”)| select(.IsLatest == false) | select(.Size>0) | select (.VersionId != “null”) | “aws s3api get-object —bucket BUCKETNAME —version-id \"" + .VersionId + ”\” —key \"" + .Key + ”\” \"" + .Key + ”\""’ | sh

Explained:

This will download all versions of all such files. If a file has been replaced multiple times, the most recent version of that file will remain on the disk.

It is required that the paths have been pre-created with “mkdir -p” before executing this. This is because get-object doesn’t create subfolders when storing output files.



Previous Post
Apple pitää lehtitilaajan puolta
Next Post
Elisa Kirja + Kindle = win