Skip to content

Instantly share code, notes, and snippets.

@apaleslimghost
Forked from i-like-robots/upload-to-s3.sh
Last active October 3, 2019 11:58
Show Gist options
  • Save apaleslimghost/4e8bbb7391455d15dbe40be74150dcd3 to your computer and use it in GitHub Desktop.
Save apaleslimghost/4e8bbb7391455d15dbe40be74150dcd3 to your computer and use it in GitHub Desktop.
Upload client-side assets to S3 via AWS CLI
#!/bin/bash
DESTINATION_BUCKET="hashed-assets-eu"
DESTINATION_FOLDER="page-kit"
upload_file() {
local FILE="$1"
local ENCODING
local TYPE
if [[ "$FILE" == *".gz" ]]; then
ENCODING="gzip"
elif [[ "$FILE" == *".br" ]]; then
ENCODING="br"
fi
# the AWS CLI can guess content types but not the original type of compressed files
# <https://github.com/aws/aws-cli/issues/3817>
case "$FILE" in
*".js"|*".js.gz"|*".js.br")
TYPE="application/javascript"
;;
*".css"|*".css.gz"|*".css.br")
TYPE="text/css"
;;
*".map")
TYPE="application/octet-stream"
;;
esac
aws s3 cp $FILE "s3://$DESTINATION_BUCKET/$DESTINATION_FOLDER/$FILE" \
--cache-control=31536000 \
--content-type="$TYPE; charset=utf-8" \
--content-encoding="$ENCODING" \
--acl="public-read"
}
upload_files() {
local SOURCE_FILES=($(find public/*.{js,css,gz,br,map} 2>/dev/null))
for FILE in ${SOURCE_FILES[@]}; do
upload_file "$FILE"
done
}
upload_files
@CLTPayne
Copy link

CLTPayne commented Oct 2, 2019

👍Two questions:

  1. With the current page-kit-cli the output directory is configurable. So wondering if the public directory should be a parameter that's passed in? Or with the new vision of the cli package, will this become a fixed directory name for the webpack config?
  2. Following on from that, if for some other reason the public folder can't be found would some kind of error handling be handy? Tried this out with no public and wondered if “SOURCE_FILES[@]: unbound variable” could be more useful with some context like "'/pubic directory not found" if thrown in the Circle Ci console?

What do you think?

@apaleslimghost
Copy link
Author

those are both very good suggestions 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment