Created
July 14, 2015 20:38
-
-
Save hagbarddenstore/12e907174927db8a03f3 to your computer and use it in GitHub Desktop.
A Bash script to retrieve a file stored on S3 via temporary IAM role credentials.
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
#!/bin/bash | |
if [ $# -ne 3 ] | |
then | |
echo "Usage: s3-get iam-role-name s3-resource output-file" | |
exit 1 | |
fi | |
Role=$1 | |
Resource=$2 | |
Output=$3 | |
AccessKeyId=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${Role} | sed -n '/AccessKeyId/{p;}' | cut -f4 -d'"'` | |
SecretAccessKey=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${Role} | sed -n '/SecretAccessKey/{p;}' | cut -f4 -d'"'` | |
Token=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${Role} | sed -n '/Token/{p;}' | cut -f4 -d'"'` | |
Date="`date +'%a, %d %b %Y %H:%M:%S %z'`" | |
SignatureString="GET\n\n\n${Date}\nx-amz-security-token:${Token}\n/${Resource}" | |
Signature=`/bin/echo -en "${SignatureString}" | openssl sha1 -hmac ${SecretAccessKey} -binary | base64` | |
curl -s -H "Date: ${Date}" -H "X-AMZ-Security-Token: ${Token}" -H "Authorization: AWS ${AccessKeyId}:${Signature}" "https://s3-eu-west-1.amazonaws.com/${Resource}" -o "${Output}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AWESOME!!! Thank you, don't think you have been given enough credit!!