Skip to content

Instantly share code, notes, and snippets.

@artbikes
Forked from UnixSage/send2s3.sh
Created May 14, 2020 13:55
Show Gist options
  • Save artbikes/1afad865ded4ea1e193c8e6b80f175f3 to your computer and use it in GitHub Desktop.
Save artbikes/1afad865ded4ea1e193c8e6b80f175f3 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 $2}'`
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