Skip to content

Instantly share code, notes, and snippets.

@leonidlm
Created December 11, 2014 11:24
Show Gist options
  • Save leonidlm/a2c2025f3aa89ae573e2 to your computer and use it in GitHub Desktop.
Save leonidlm/a2c2025f3aa89ae573e2 to your computer and use it in GitHub Desktop.
fetch_s3_file() {
local bucket=$1
local file_path=$2
local iam_policy=$3
local metadata_path="169.254.169.254/latest/meta-data/iam/security-credentials"
CREDENTIALS=$(curl http://$metadata_path/$iam_policy/)
key_id=$(echo $CREDENTIALS | jq '.AccessKeyId' | sed 's/"//g')
key_secret=$(echo $CREDENTIALS | jq '.SecretAccessKey' | sed 's/"//g')
session_token=$(echo $CREDENTIALS | jq '.Token' | sed 's/"//g')
date="$(LC_ALL=C date -u +"%a, %d %b %Y %X %z")"
sig="$(printf "GET\n\n\n$date\n/$bucket/$file_path" | openssl sha1 -binary -hmac "$key_secret" | base64)"
curl -XGET https://s3.amazonaws.com/$file_path \
-O \
-H "Date: ${date}" \
-H "x-amz-security-token: $session_token" \
-H "Authorization: AWS $key_id:$sig"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment