Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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