Skip to content

Instantly share code, notes, and snippets.

@UnixSage
Last active March 5, 2021 01:58
Show Gist options
  • Save UnixSage/07d85e09bb50a178500c20463b81ba81 to your computer and use it in GitHub Desktop.
Save UnixSage/07d85e09bb50a178500c20463b81ba81 to your computer and use it in GitHub Desktop.
Simple shell script that uses curl to send a file to s3. Useful when you do not want to load the awscli for a simple job. May have to adjust mime types according to need.
#!/bin/sh
if [ $# -ne 1 ] ; then
echo "Need File Name"
exit
fi
s3AccessKey="##ACCESS##"
s3SecretKey="##SECRET##"
s3Bucket="data.example.com"
#Strips path from file so it is not created on S3
DIR=`dirname $1`
FILE=`basename $1`
cd $DIR
fileName=$FILE
EXT=`basename $1 | awk -F"." '{print $NF}'`
case $EXT in
html)
contentType="text/html"
;;
css)
contentType="text/css"
;;
js)
contentType="text/javascript"
;;
mkv)
contentType="video/x-matroska"
;;
*)
contentType="application/octet-stream"
;;
esac
date=`date +%Y%m%d`
dateFormatted=`date -R`
relativePath="/${s3Bucket}/${fileName}"
stringToSign="PUT\n\n${contentType}\n${dateFormatted}\n${relativePath}"
signature=`echo -en ${stringToSign} | openssl sha1 -hmac ${s3SecretKey} -binary | base64`
curl -o /dev/null -X PUT -T "${fileName}" \
-H "Host: ${s3Bucket}.s3.amazonaws.com" \
-H "Date: ${dateFormatted}" \
-H "Content-Type: ${contentType}" \
-H "Authorization: AWS ${s3AccessKey}:${signature}" \
http://${s3Bucket}.s3.amazonaws.com/${fileName}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment