./create-s3-website.sh -n my.test.bucket -r eu-central-1 -d build
By default, the
-e <error-document
option isindex.html
, unless specified
#!/usr/bin/env bash | |
usage() { echo "Usage: $0 -n <bucket> -r <region> -d <directory> [-e <error-document>]" 1>&2; exit 1; } | |
while getopts ":n:r:d:" o; do | |
case "${o}" in | |
n) | |
n=${OPTARG} | |
;; | |
r) | |
r=${OPTARG} | |
;; | |
d) | |
d=${OPTARG} | |
;; | |
e) | |
e=${OPTARG} | |
;; | |
*) | |
usage | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
if [ -z "${n}" ] || [ -z "${r}" ] || [ -z "${d}" ]; then | |
usage | |
fi | |
uri=s3://"${n}" | |
policy={\"Version\":\"2012-10-17\",\"Statement\":[{\"Sid\":\"PublicReadGetObject\",\"Effect\":\"Allow\",\"Principal\":\"*\",\"Action\":\"s3:GetObject\",\"Resource\":[\"arn:aws:s3:::${n}/*\"]}]} | |
aws s3 mb $uri --region ${r} | |
if [ -z "${e}" ]; then | |
aws s3 website ${uri} --index-document index.html --error-document index.html | |
else | |
aws s3 website ${uri} --index-document index.html --error-document ${e} | |
fi | |
aws s3api put-bucket-policy --bucket ${n} --policy ${policy} | |
aws s3 sync ${d} ${uri} | |
echo -e "\nhttp://${n}.s3-website.${r}.amazonaws.com\n" |