Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save drfill/c18308b6d71ee8032efda870b9be348e to your computer and use it in GitHub Desktop.
Save drfill/c18308b6d71ee8032efda870b9be348e to your computer and use it in GitHub Desktop.
Amazon S3 download with Curl
#!/bin/sh
file=path/to/file
bucket=your-bucket
resource="/${bucket}/${file}"
contentType="application/x-compressed-tar"
dateValue="`date +'%a, %d %b %Y %H:%M:%S %z'`"
stringToSign="GET
${contentType}
${dateValue}
${resource}"
s3Key=xxxxxxxxxxxxxxxxxxxx
s3Secret=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
signature=`/bin/echo -en "$stringToSign" | openssl sha1 -hmac ${s3Secret} -binary | base64`
curl -H "Host: ${bucket}.s3.amazonaws.com" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: AWS ${s3Key}:${signature}" \
https://${bucket}.s3.amazonaws.com/${file}
@dashxdr
Copy link

dashxdr commented Oct 17, 2018

This doesn't work. You have spaces on the end of every line that need to be deleted. And you need a 2nd newline after line 7, after the GET.

@aarongooch
Copy link

@dashxdr is right stringToSign should be

stringToSign="GET\n\n${contentType}\n${dateValue}\n${resource}"

@layer4down
Copy link

Thanks for the script and the helpful comments - worked like a charm! 👍

@impl1874
Copy link

impl1874 commented May 8, 2021

bucket path style.
use "/bin/echo" wrong in my shell.

#!/bin/bash
file=$1
dateValue=$(date -R -u +'%a, %d %b %Y %H:%M:%S %z')
stringToSign="GET\n\n\n${dateValue}\n${file}"
signature=$(echo -en "${stringToSign}" | openssl sha1 -hmac "${S3_SECRET_KEY}" -binary | base64)
curl "https://${S3_HOST}${file}" "${@:2}"
-X GET
-H "Host: ${S3_HOST}"
-H "Date: ${dateValue}"
-H "Authorization: AWS ${S3_ACCESS_KEY}:${signature}"

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