#!/bin/bash BUCKET=tfscript.nickhope.world AWSROLE="arn:aws:iam::096320175891:role/Admin" NORMALAGE=120 # in seconds # could be something like "foo" if everything is meant to be in /foo PREFIX="" AWS="aws" # for simple debugging, change this to AWS="echo aws" AWSLOG="sync.log" export AWS_DEFAULT_OUTPUT=text export AWS_PAGER= CACHE="--cache-control max-age=\"${NORMALAGE}\",public" # Set up AWS creds. Assume role once. Reuse creds after that. temp_role=$(aws --output json sts assume-role \ --role-arn "${AWSROLE}" \ --role-session-name "tfscript-sync") export AWS_ACCESS_KEY_ID=$(echo $temp_role | jq -r .Credentials.AccessKeyId) export AWS_SECRET_ACCESS_KEY=$(echo $temp_role | jq -r .Credentials.SecretAccessKey) export AWS_SESSION_TOKEN=$(echo $temp_role | jq -r .Credentials.SessionToken) rm -f ${AWSLOG} ${AWS} s3 sync --only-show-errors \ build/html s3://${BUCKET} --delete \ ${CACHE} 2>&1 >> ${AWSLOG} echo "Writing index files to directory markers" time (find build/html/* -name index.html | while read -r line; do # ${AWS} s3 cp $line "s3://$BUCKET/${BASH_REMATCH[1]}" # get rid of "public/" OBJKEY=${line#build\/html/*} # get rid of "/index.html" OBJKEY=${OBJKEY%/index.html} echo "Uploading from $line to s3://${BUCKET}/${OBJKEY}/" ${AWS} s3api put-object --bucket "$BUCKET" \ --body ${line} --key "${OBJKEY}" --content-type text/html ${CACHE} 2>&1 >> ${AWSLOG} ${AWS} s3api put-object --bucket "$BUCKET" \ --body ${line} --key "${OBJKEY}/" --content-type text/html ${CACHE} 2>&1 >> ${AWSLOG} done)