Skip to content

Instantly share code, notes, and snippets.

@patrickshan
Last active June 29, 2017 04:49
Show Gist options
  • Save patrickshan/bb8c06e7ef55ba3889db7ad0e4efd240 to your computer and use it in GitHub Desktop.
Save patrickshan/bb8c06e7ef55ba3889db7ad0e4efd240 to your computer and use it in GitHub Desktop.
#!/bin/bash
instance_profile=`curl http://169.254.169.254/latest/meta-data/iam/security-credentials/`
aws_access_key_id=`curl http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | grep AccessKeyId | cut -d':' -f2 | sed 's/[^0-9A-Z]*//g'`
aws_secret_access_key=`curl http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | grep SecretAccessKey | cut -d':' -f2 | sed 's/[^0-9A-Za-z/+=]*//g'`
token=`curl -s http://169.254.169.254/latest/meta-data/iam/security-credentials/${instance_profile} | sed -n '/Token/{p;}' | cut -f4 -d'"'`
region="ap-southeast-2"
file="test.sh"
bucket="test.bucket"
date="`date -u +%Y%m%dT%H%M%SZ`"
resource="/${bucket}/${file}"
signature_string="GET\n\n\n${date}\nx-amz-security-token:${token}\n/${resource}"
signature=`/bin/echo -en "${signature_string}" | openssl sha1 -hmac ${aws_secret_access_key} -binary | base64`
authorization="AWS ${aws_access_key_id}:${signature}"
curl -L -s -H "Date: ${date}" -H "X-AMZ-Security-Token: ${token}" -H "Authorization: ${authorization}" "https://s3-${region}.amazonaws.com/${resource}" -o "test.sh"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment