Skip to content

Instantly share code, notes, and snippets.

@sgnn7
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgnn7/1574cc5dc9f0495725c9 to your computer and use it in GitHub Desktop.
Save sgnn7/1574cc5dc9f0495725c9 to your computer and use it in GitHub Desktop.
Upload file to S3
#!/bin/bash -e
if [ $# -ne 2 ]; then
echo "Usage: $0 <file> <s3keysecret>"
exit 1
fi
if [ ! -f $1 ]; then
echo "File $1 not found. Exiting"
exit 1
fi
s3Key="redacted"
s3Secret=$2
file=$1
bucket="redacted"
filename=$(basename $file)
resource="/${bucket}/${filename}"
contentType="application/x-compressed-tar"
dateValue=$(date -R)
stringToSign="PUT\n\n${contentType}\n${dateValue}\n${resource}"
signature=$(echo -en ${stringToSign} | openssl sha1 -hmac ${s3Secret} -binary | base64)
echo -n "Uploading..."
curl -X PUT --data @${file} \
-H "Host: ${bucket}.s3.amazonaws.com" \
-H "Date: ${dateValue}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: AWS ${s3Key}:${signature}" \
https://${bucket}.s3.amazonaws.com/${filename}
echo "OK"
echo " S3 -> https://s3.amazonaws.com$resource"
echo " CloudFront (preferred) -> https://something.cloudfront.net/$filename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment