Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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