Last active
March 20, 2018 14:40
-
-
Save axos88/b06a5ada711c23782cb83051acd3230b to your computer and use it in GitHub Desktop.
Download all versions of a specific object in S3
This file contains 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
echo "Usage - $0 BUCKET KEY SUFFIX BATCH_SIZE" | |
BUCKET=$1 | |
KEY=$2 | |
SUFFIX=$3 | |
BATCH=${4:- 25} | |
echo "Querying versions..." | |
VERSIONS=$(aws s3api list-object-versions --bucket $1 --prefix $2 | jq '.Versions[].VersionId' -r) | |
CNT=0 | |
for I in $VERSIONS; do | |
CNT=$(( $CNT + 1 )) | |
done | |
echo "Found $CNT versions" | |
IDX=0 | |
for V in $VERSIONS; do | |
IDX=$(( $IDX + 1 )) | |
echo "($IDX/$CNT): Start fetching $V..." | |
( | |
FN=$( | |
aws s3api get-object --bucket $BUCKET --key $KEY --version-id $V tmp$IDX | | |
jq '.LastModified' -r | | |
awk 'BEGIN { OFS = "-" } { print $4,$3,$2,$5 } ' | | |
tr ':' '-' | |
) && | |
mv tmp$IDX $FN$SUFFIX && | |
echo "Done fetching $V - $FN" | |
) & | |
if [ $(($IDX % $BATCH)) -eq 0 ]; then | |
wait | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You will need to have
aws
,jq
andawk
installed for this to work. Will make 25 parallel requests to fetch files by default