Skip to content

Instantly share code, notes, and snippets.

@pkskelly
Created December 22, 2017 01:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkskelly/72e5e609afef5a5a8e61d934db905a10 to your computer and use it in GitHub Desktop.
Save pkskelly/72e5e609afef5a5a8e61d934db905a10 to your computer and use it in GitHub Desktop.
Azure CLI script to enable Blob storage containers to serve Angular static files.
#!/bin/bash
AZURE_STORAGE_ACCOUNT=""
AZURE_STORAGE_ACCESS_KEY=""
while getopts "a:k:" opt; do
case $opt in
a)
AZURE_STORAGE_ACCOUNT="${OPTARG}"
;;
k)
AZURE_STORAGE_ACCESS_KEY="${OPTARG}"
;;
\?)
exit 1
;;
esac
done
if [[ ! $AZURE_STORAGE_ACCOUNT ]] || [[ ! $AZURE_STORAGE_ACCESS_KEY ]]; then
echo "Missing command line options. "
echo " Example: $0 -a [storage_account] -k [access key]!" >&2; exit 1;
fi
# Container names and build output locations
root_container="\$root"
assets_container="assets"
build_root_dir="$(pwd)/dist"
build_assets_dir="${build_root_dir}/${assets_container}"
# Show the Azure Subscription which is active
az account show
echo "Stoage Account: $AZURE_STORAGE_ACCOUNT"
echo "Account Key : $AZURE_STORAGE_ACCESS_KEY"
# Create the $root container
echo "Creating the ${root_container} container..."
az storage container create --name "${root_container}" --account-name $AZURE_STORAGE_ACCOUNT --account-key $AZURE_STORAGE_ACCESS_KEY --public-access blob --verbose
echo "Uploading the ${root_container} files..."
for entry in "${build_root_dir}"/*
do
if [ ! -d $entry ]; then
filename="${entry##*/}"
echo "Uploading $filename..."
az storage blob upload --container-name ${root_container} --file $entry --name $filename --account-name $AZURE_STORAGE_ACCOUNT --account-key $AZURE_STORAGE_ACCESS_KEY
fi
done
# Create the assets container
echo "Creating the ${assets_container} container..."
az storage container create --name "${assets_container}" --account-name $AZURE_STORAGE_ACCOUNT --account-key $AZURE_STORAGE_ACCESS_KEY --public-access blob --verbose
echo "Uploading the $assets_container files..."
for entry in "${build_assets_dir}"/*
do
if [ ! -d $entry ]; then
filename="${entry##*/}"
echo "Uploading $filename..."
az storage blob upload --container-name ${assets_container} --file $entry --name $filename --account-name $AZURE_STORAGE_ACCOUNT --account-key $AZURE_STORAGE_ACCESS_KEY
fi
done
echo "Container creation complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment