Created
November 13, 2018 20:22
-
-
Save saii9/210ecb196e96bc2c4976ba40c03817e6 to your computer and use it in GitHub Desktop.
download file from s3 using curl v4 authentication
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 | |
set -e | |
# set -x | |
function log(){ | |
echo "$(date -u +'%Y%m%dT%H%M%SZ') - $*" | |
} | |
# variable declaration - start | |
bucket=<bucket> | |
access_key_id=<access_key_id> | |
secret_access_key=<secret_access_key> | |
amzFile=<file_to_download> | |
aws_region="us-east-1" | |
outputFile=file.tar.gz | |
dateValueS=$(date -u +'%Y%m%d') | |
dateValueL=$(date -u +'%Y%m%dT%H%M%SZ') | |
#emptySha=`echo -n ""|sha256sum|sed 's/\s*\*-//'` #windows | |
emptySha=`echo -n ""|sha256sum|sed 's/\s*-//'` #Linux | |
# variable declaration - end | |
log "downloading $amzFile" | |
# getting file form s3 - start | |
#creating a canonical request | |
log "creating canonical request" | |
echo GET > cform.txt | |
echo /${amzFile}>> cform.txt | |
echo >> cform.txt | |
echo "host:${bucket}.s3.amazonaws.com">> cform.txt | |
echo "x-amz-content-sha256:${emptySha}">> cform.txt | |
echo "x-amz-date:${dateValueL}">> cform.txt | |
echo >> cform.txt | |
echo "host;x-amz-content-sha256;x-amz-date" >> cform.txt | |
echo -n ${emptySha}>> cform.txt | |
#cp cform.txt req.txt | |
#taking hash of the canonical request | |
#canonicalRequestHash=`echo -n ${canonicalRequest}|sha256sum |sed 's/\s*\*-//'` | |
canonicalRequestHash=`sha256sum cform.txt|sed 's/\s.*$//'` | |
#creating string to sign | |
log "creating string to sign" | |
echo AWS4-HMAC-SHA256 > cform.txt | |
echo ${dateValueL} >> cform.txt | |
echo ${dateValueS}/us-east-1/s3/aws4_request >> cform.txt | |
echo -n ${canonicalRequestHash} >> cform.txt | |
function hmac_sha256 { | |
key="$1" | |
data="$2" | |
echo -n "$data" | openssl dgst -sha256 -mac HMAC -macopt "$key" | sed 's/^.* //' | |
} | |
#creating an authorization string | |
log "creating auth string" | |
dateKey=$(hmac_sha256 key:"AWS4$secret_access_key" $dateValueS) | |
dateRegionKey=$(hmac_sha256 hexkey:$dateKey $aws_region) | |
dateRegionServiceKey=$(hmac_sha256 hexkey:$dateRegionKey s3) | |
signingKey=$(hmac_sha256 hexkey:$dateRegionServiceKey "aws4_request") | |
signature=`openssl dgst -sha256 -mac HMAC -macopt hexkey:${signingKey} cform.txt| sed 's/^.* //'` | |
rm -f cform.txt | |
#signature=$(awsStringSign4 "${secret_access_key}" "${dateValueS}" "${aws_region}" s3 "${stringToSign}") | |
#curl to s3 to get the file | |
log "curling to https://${bucket}.s3.amazonaws.com/${amzFile}" | |
curl -H "Authorization: AWS4-HMAC-SHA256 Credential=${access_key_id}/${dateValueS}/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=${signature}" \ | |
-H "host: ${bucket}.s3.amazonaws.com" \ | |
-H "x-amz-content-sha256: $emptySha" \ | |
-H "x-amz-date: ${dateValueL}"\ | |
https://${bucket}.s3.amazonaws.com/${amzFile} -o $outputFile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment