Last active
November 10, 2018 00:03
-
-
Save aimtiaz11/04b33a39638aa2110ac1cb8509470a4f to your computer and use it in GitHub Desktop.
Script to upload all (html) files of a directory to AWS S3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script uploads all HTML file and uploads them in AWS s3 by including the directory structure. | |
# Ensure bucket is created via TF before adding to array below | |
envList=("mock" "dev" "test" "uat") | |
# Helper function to check if value exists in array | |
function contains() { | |
local n=$# | |
local value=${!n} | |
for ((i=1;i < $#;i++)) { | |
if [ "${!i}" == "${value}" ]; then | |
echo "y" | |
return 0 | |
fi | |
} | |
echo "n" | |
return 1 | |
} | |
################################## | |
## START OF SCRIPT | |
################################## | |
# $1 is env, check if arg $1 (env) exists in the array (env list) above | |
if [ $(contains "${envList[@]}" $1) == "y" ]; then | |
env=$1 | |
echo "Uploading files to bucket $env" | |
else | |
echo "Unsupported env" | |
echo "Supported environments are " | |
for i in ${envList[@]}; do echo $i; done | |
printf "Please create an S3 bucket and update this script's 'envList' array\n" | |
exit 1; | |
fi | |
# Name of your S3 bucket | |
bucketName=mycomp-int-$env-example-bucket | |
# Additional validation - does bucket actually exist in S3 despite being added in above array? | |
echo "Checking if bucket exists...." | |
aws s3 ls s3://$bucketName | |
if [ $? -eq 0 ]; then | |
# Empty the bucket content | |
printf "Bucket exists. Emptying content.." | |
aws s3 rm s3://$bucketName --recursive | |
for file in $(find s3/* -name "*html"); | |
do | |
read objectKey <<< $(printf $file | awk '{ | |
n=split($0, arr , "/"); | |
for(i=2; i < n;i++) | |
if(objectKey=="") | |
objectKey=arr[i] | |
else | |
objectKey=objectKey"/"arr[i] | |
print objectKey | |
}') | |
read fileName <<< $(printf $file | awk '{ | |
n=split($0, arr , "/"); | |
if(arr[n] ~ /\.html$/) | |
print arr[n]; | |
else | |
exit 1; | |
}') | |
echo ">> Uploading $directory/$file to s3://$bucketName" | |
aws s3api put-object --bucket $bucketName --key $objectKey/$fileName --body s3/$objectKey/$fileName | |
echo ">>> done" | |
done | |
else | |
echo "Bucket s3://$bucketName might not exist or inaccessible due to insufficient permissions - not uploading anything." | |
exit 1; | |
fi | |
echo "Summary of content in the s3 bucket" | |
aws s3 ls s3://$bucketName --recursive --human-readable --summarize | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment