Skip to content

Instantly share code, notes, and snippets.

@axos88
Last active March 20, 2018 14:40
Show Gist options
  • Save axos88/b06a5ada711c23782cb83051acd3230b to your computer and use it in GitHub Desktop.
Save axos88/b06a5ada711c23782cb83051acd3230b to your computer and use it in GitHub Desktop.
Download all versions of a specific object in S3
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
@axos88
Copy link
Author

axos88 commented Mar 20, 2018

You will need to have aws,jq and awk installed for this to work. Will make 25 parallel requests to fetch files by default

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