S3/CloudFront Site Uploader


Recommended Posts

If you are looking at using Amazon S3/CloudFront to host a static website, I have come up with a nice little script (requires s3cmd from http://s3tools.org/s3cmd) that allows you to create a bucket and distribution for your site in a relatively seamless fashion.

What it does:

  • Creates an S3 bucket (or uses an existing one)
  • Uploads ALL of the files within a specified folder tree into the bucket
  • Makes all of the items "public"
  • Optionally creates a CloudFront distribution and sets a CNAME and default root object (e.g. index.html) based on your input.

Below is the script. Obviously you're going to need s3cmd and an existing Amazon Web Services account with S3 and CloudFront enabled. The result will ultimately be a fast CDN at Amazon's very reasonable price.

Be sure to save this as a bash script and set it to executable (chmod +x).


clear
echo "S3/CloudFront Website Uplaoder"
echo
echo "This utility will take advantage of AWS to host a website."
echo "The end result will be a cost-effective CDN-based website."
echo
echo "Please note:"
echo " - You must have an existing AWS account w/ S3 + CF"
echo " - You must have s3cmd 1.0.0 + installed on this computer"
echo " - Your API info can be found in your AWS account settings"
echo " - All files will be public and filetypes will be guessed"
echo " - An S3 bucket will be created if it doesn't exist"
echo " - A CloudFront distribution can be created if specified"
echo
echo "--------------------------------------------------------------"
echo
echo "Please enter your API key:"
read API_KEY
echo
echo "Please enter your API secret:"
read API_SEC
echo
echo "Please enter the local folder that contains your site:"
read LOC_DIR
echo
echo "Please enter the S3 bucket where the site will be uploaded to:"
read REM_BUC
echo
echo "Set up a CloudFront distribution - 'true' or 'false'"
echo "Use 'true' if you want to take advantage of edge locations or default root objects."
echo "DO NOT use 'true' if you have an existing distribution on the given bucket."
read CFT_SET
if [ "$CFT_SET" = "true" ]; then
echo
echo "Please enter the default root object (index.html) for ClodFront:"
read CFT_DRE
echo
echo "Please enter the CNAME that you will use for the CF distribution:"
read CFT_CNM
fi
echo
echo "--------------------------------------------------------------"
echo
echo "Checking to make sure that all of the needed info was input..."
echo
if [ "$API_KEY" != "" ] && [ "$API_SEC" != "" ] && [ "$LOC_DIR" != "" ] && [ "$REM_BUC" != "" ]; then
echo "All of your information appears to have been entered."
echo "Mind you, the validity of this information has yet to be validated."
echo
echo "--------------------------------------------------------------"
echo
else
echo "YOU ARE MISSING FIELD DATA. TRY AGAIN."
echo
echo "--------------------------------------------------------------"
echo
exit
fi
echo "Making sure that your local directory is valid..."
echo
if [ -e $LOC_DIR ]; then
echo "Local directory has been validated!"
echo
echo "--------------------------------------------------------------"
echo
else
echo "THE LOCAL DIRECTORY YOU SPECIFIED DOES NOT EXIST. TRY AGAIN."
echo
echo "--------------------------------------------------------------"
echo
exit
fi
echo "Making sure that you have s3cmd installed..."
echo
if [ "`whereis s3cmd`" != "s3cmd:" ]; then
echo "s3cmd appears to be installed. All is good."
echo
echo "--------------------------------------------------------------"
echo
else
echo "S3CMD IS NOT INSTALLED. TRY AGAIN."
echo
echo "--------------------------------------------------------------"
echo
exit
fi
echo "Creating validation file..."
echo
S3_FILE="`dirname $0`/`date +%Y%m%d%H%M%S`s3cmd.tmp"
echo "$S3_FILE"
echo "access_key = $API_KEY" >> $S3_FILE
echo "secret_key = $API_SEC" >> $S3_FILE
tail $S3_FILE
echo
echo "--------------------------------------------------------------"
echo
echo "STARTING THE UPLOAD PROCESS!"
echo
s3cmd --config=$S3_FILE mb s3://$REM_BUC
s3cmd --config=$S3_FILE sync --acl-public --guess-mime-type $LOC_DIR/ s3://$REM_BUC
if [ "$CFT_SET" = "true" ]; then
s3cmd --config=$S3_FILE cfcreate s3://$REM_BUC
s3cmd --config=$S3_FILE cfmodify --cf-default-root-object=$CFT_DRE s3://$REM_BUC
CFT_DMN="`s3cmd --config=$S3_FILE cfmodify --cf-add-cname=$CFT_CNM s3://$REM_BUC | grep DomainName`"
fi
echo
echo "--------------------------------------------------------------"
echo
echo "The details for your new configuration is as follows:"
echo "S3Bucket: $REM_BUC"
if [ "$CFT_SET" = "true" ]; then
echo "$CFT_DMN"
echo "DfltRtObjt: $CFT_DRE"
echo "CNAME: $CFT_CNM"
echo
echo "You will want to create a DNS record pointing your CNAME to your CloudFront distribution ('DomainName')."
echo
echo "CloudFront takes a bit of time to set up."
echo "Patience is a virtue."
fi
echo
echo "--------------------------------------------------------------"
rm $S3_FILE

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...