
If you use Azure blob storage for static web hosting, you’ll probably want a way to automate uploading your website file content to the Azure storage account. According to Microsoft’s official document, the Azure Storage platform is Microsoft’s cloud storage solution for modern data storage scenarios. Azure Storage offers highly available, massively scalable, durable, and secure storage for a variety of data objects in the cloud. Here are some interesting guides: how to configure Azure Monitor for VMs on Azure Stack Hub and How To Use Azure Key Vault secrets in Azure Pipelines.
Before now, one good option we normally used was the AzCopy utility. Fortunately, the az storage blob upload-batch
command in the Azure CLI now allows for bulk file upload. You must place these in a container called $web (so make sure you surround that with single quotes in PowerShell). The $web container is created automatically as soon as you create your Storage Account on Azure. You also specify the name of the folder that will contain the files to be uploaded. Here we are uploading a full website application stored in a folder known as webfiles
.
To learn more about Azure CLI, PowerShell Cmdlets, and Azure in general, see the following guides; how to use the Azure Cloud Shell, Azure CLI, and Azure PowerShell, How to Remove Azure VM: How to delete a Virtual Machine via the Azure Portal, and Azure virtual networks: How to prepare Azure and on-premises virtual networks using Azure CLI commands,
Please keep in mind that uploading files to Azure Storage using CLI will not delete any existing files.
The Azure command-line interface (Azure CLI) is a set of commands used to create and manage Azure resources. The Azure CLI is available across Azure services and is designed to get you working quickly with Azure, with an emphasis on automation.
Uploading Bulk Files to Azure Storage Account
To get along in this tutorial, the following prerequisites are necessary:
- Active Azure Subscription. If you don’t one, you can sign up here
- Azure CLI for Windows. You can download and install Azure CLI for Windows here
- Azure Storage Account with $web container created
- Clone or download a free website template and store the folder in the Desktop location on your PC
If you have the above requirements, proceed with me to the next steps.
Step 1 – Login into Azure Portal via Azure CLI for Windows
To do this, launch PowerShell on your PC. When the PowerShell Cmdlet Console opens up, type:
az login
After typing az login
and press enter, a browser authentication window pops up requiring you to enter the email and password associated with your Microsoft Azure Account. Once you enter these details, you will be automatically logged in.
Step 2 – Create Azure Resources such as Resource Group and Azure Storage Account
Create a Resource Group which is a logical folder where resources on Azure are stored. To create a Resource Group using the Azure CLI, run:
az group create --name $ResourceGroup --location $Region
The next thing is to create an Azure Storage Account. To do so, run:
az storage account create --name $StorageAccountName --resource-group $ResourceGroup --location $Region --sku $StorageAccountSku --kind StorageV2
Note that Storage Account is a global resources on Azure. So the name must give a unique name that is not already in existence. If you the name has been taken already, you will receive an prompt while creating it.
Step 3 – Enable static website hosting for your storage account
Before you can run a static website on your Azure Storage Account you must first of all enable the static website hosting feature for the blob storage. To do so, run:
az storage blob service-properties update --account-name $StorageAccountName --static-website --index-document index.html
To confirm that the storage account has been enabled for static website hosting and also that the $web container has been created when we created the storage account, let’s head to the Azure Portal to check.
When you’re in the Azure Portal, locate and click on "Resource Groups"
In the list of Resource Groups, the display locate the name of the Resource Group you created to hold your Resources and locate the storage account you created earlier. Here, the name of our Resource Group is webfilesRG
while the storage account name is webfilestorage1
At this point, double-click to open the storage account dashboard, locate and click on static website
tab in the storage account blade to see that it has been enabled. In there, you will also find that the $web
container has been created. Below the $web
container is the primary endpoint. The primary endpoint is the URL that will be accessible to your website visitors.
Step 4 – Upload website folders from the local path
To upload the website files, you will first of changing the working directory to the directory where your website files folder is located on your local PC with cd <DirectoryName>
the command. Here is the folder container our website files is located on the Desktop of our local PC.
If you have changed the working directory, run:
az storage blob upload-batch --s $LocalPath --d '$web' --account-name $StorageAccountName
After the bulk files upload has been complete, your output screen should look like the below screenshot.
Now, head back to the $web container
by clicking on the container tab under Data Storage
in the storage account blade on the Azure Portal to open it and assess your website files.
Step 5 – Display the website URL
To display the website URL, run:
az storage account show -n $StorageAccountName --resource-group $ResourceGroup --query "primaryEndpoints.web" --output tsv
The website URL is displayed as shown in the screenshot above. Visiting the URL will take visitors to the website page.
You’re free to the customer the free template to reflect your information. As shown in the screenshot above, I have customized the information to reflect my information.
Clean up Azure Resources
To avoid being charged for resources running on your subscription in Azure, delete the resources you have just created. To do so, we will delete the Resource Group that contains all the resources we have used for the exercise.
Run:
az group delete --resource-group <ResourceGroupName>
When prompted "Are you sure you want to perform this operation? (y/n): type Y"
Congrats! You’ve just learned how to create a Resource Group, an Azure storage account, enable a storage account for a static website, upload bulk files to an Azure blob storage container, and display the website URL using Azure CLI.
Be on the lookout for another insightful guide! I hope you found this blog post helpful. Please let me know in the comment session if you have any questions.