Skip to content

Instantly share code, notes, and snippets.

@eurica
Created May 1, 2013 00:46
Show Gist options
  • Save eurica/5493017 to your computer and use it in GitHub Desktop.
Save eurica/5493017 to your computer and use it in GitHub Desktop.
A bash script to upload files for static hosting on s3
# Bash script to upload files to static s3 hosting with s3cp
# dave@pagerduty.com April 30th, 2013
# Modified from https://github.com/roqs23/sync-svn-updates-to-s3/blob/master/svnupd2s3.sh
export COMMON_HEADER="--header \"x-amz-acl: public-read\" --header \"Vary: Accept-Encoding\" "
export HTML_HEADER="--header \"Cache-Control: max-age=604800,must-revalidate\" --header \"Content-Type: text/html\""
export CSS_HEADER="--header \"Cache-Control: max-age=604800,must-revalidate\" --header \"Content-Type: text/css\""
export JS_HEADER="--header \"Cache-Control: max-age=604800,must-revalidate\" --header \"Content-Type: application/javascript\""
export PNG_HEADER="--header \"Cache-Control: max-age=2592000,must-revalidate\" --header \"Content-Type: image/png\""
export GIF_HEADER="--header \"Cache-Control: max-age=2592000,must-revalidate\" --header \"Content-Type: image/gif\""
export JPG_HEADER="--header \"Cache-Control: max-age=2592000,must-revalidate\" --header \"Content-Type: image/jpeg\""
export HTMLGZ_HEADER="--header \"Cache-Control: max-age=604800,must-revalidate\" --header \"Content-Type: text/html\" --header \"Content-Encoding: gzip\""
export CSSGZ_HEADER="--header \"Cache-Control: max-age=604800,must-revalidate\" --header \"Content-Type: text/css\" --header \"Content-Encoding: gzip\""
export JSGZ_HEADER="--header \"Cache-Control: max-age=604800,must-revalidate\" --header \"Content-Type: application/javascript\" --header \"Content-Encoding: gzip\""
#export AWS_ACCESS_KEY_ID="snipped"
#export AWS_SECRET_ACCESS_KEY="snipped"
#export S3_BUCKET="snipped"
#export WORKSPACE="public"
for LINE in `find $WORKSPACE -not -name '*.gz' -name '*.*'`
do
echo $LINE
FILE=${LINE#$WORKSPACE/}
echo $FILE
echo FILEEXT: $FILEEXT FILE: $FILE
FILEEXT=`echo $FILE|awk -F . '{print toupper($NF)}'`
HEADERVAR=`echo ${FILEEXT}_HEADER` 2>/dev/null
HTTPHEADER=${!HEADERVAR} 2>/dev/null
HEADERVAR=`echo ${FILEEXT}GZ_HEADER` 2>/dev/null
HTTPGZHEADER=${!HEADERVAR} 2>/dev/null
#echo $HTTPHEADER
#echo $HTTPGZHEADER
#echo Copying $FILE to S3 bucket
if [ "$HTTPHEADER" = "" ]; then
echo warning: no http header assigned for file extension $FILEEXT
s3cp ${WORKSPACE}/${FILE} s3://${S3_BUCKET}/${FILE}
else
#echo using --header "$HTTPHEADER"
CPCMD="s3cp ${WORKSPACE}/${FILE} s3://${S3_BUCKET}/${FILE} $HTTPHEADER $COMMON_HEADER "
eval $CPCMD
fi
#Todo: This shouldn't be necessary:
s3mod s3://${S3_BUCKET}/${FILE} public-read
if [ $FILEEXT == "HTML" -o $FILEEXT == "CSS" -o $FILEEXT == "JS" ]; then
#echo Copying compressed version for $FILE to S3 bucket
cat ${WORKSPACE}/$FILE | gzip > ${WORKSPACE}/${FILE}.gz
if [ "$HTTPGZHEADER" = "" ]; then
echo warning: no http header assigned for file extension ${FILEEXT}.gz
s3cp ${WORKSPACE}/${FILE}.gz s3://${S3_BUCKET}/${FILE}.gz
else
CPCMD="s3cp ${WORKSPACE}/${FILE}.gz s3://${S3_BUCKET}/${FILE}.gz $HTTPGZHEADER $COMMON_HEADER "
eval $CPCMD
fi
s3mod s3://${S3_BUCKET}/${FILE}.gz public-read
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment