How To Upload Files to Microsoft Azure

In this post I will show you how to upload files to an Azure storage account. This can be useful if you want to perform an offline upload of virtual hard disks, tools, or scripts.

Prepare the Storage Account

A file must be uploaded into a container in a storage account. Containers are like folders; a logical mechanism for organizing files in Azure. In my example, I want to upload a PowerShell script to Azure, so I will create a container called scripts. You can do this in two ways, the GUI or PowerShell.
In the management portal, browse into your storage account, click Containers, and click Add. Name the container, set the access level and close the wizard. Note that the name must be in lowercase.

Create a new Azure storage account container (Image credit: Aidan Finn)
Create a new Azure storage account container (Image Credit: Aidan Finn)

In case you are curious, the access option controls if and how the public can access the contents of your container:

Control access to an Azure storage account container (Image Credit: Aidan Finn)
Control access to an Azure storage account container (Image Credit: Aidan Finn)

Alternatively, you can create a new container using PowerShell. Note that before we go anywhere, we must set a default storage account for use with PowerShell. This is a little annoying; you cannot specify a storage account in each storage cmdlet. Instead you have to change the default storage account each time.

The syntax for setting a default storage account is:

Set-AzureSubscription –SubscrptionName <name of your Azure subscription> -CurrentStorageAccountName <name of the storage account>

In my example, I am working with a storage account called petridemo in a subscription called Azure in Open:

Set-AzureSubscription –SubscrptionName “Azure in Open” -CurrentStorageAccountName petridemo

Now I can use New-AzureStorageContainer to create the new container to store my future upload:

New-AzureStorageContainer –Name scripts

Upload the File

Microsoft offers you PowerShell as the solution for uploading files to an Azure storage account. There are some third-party solutions that offer a UI, but for the purposes of this article, we’ll stick with Microsoft’s method, which is based on a cmdlet called Set-AzureStorageBlobContent:

Set-AzureStorageBlobContent –Container <name of destination container in Azure> -File <path and name of file to upload> -Blob <what the file name will be in Azure>

In my example, I am going to upload C:\Temp\ServerCore.ps1 to my new scripts container in the petridemo storage account.

I have already set my default storage account using Set-AzureSubscription. Now I can run Set-AzureStorageBlobContent:

Set-AzureStorageBlobContent –Container scripts -File C:\Temp\ServerCore.ps1 -Blob ServerCore.ps1

A progress bar appears in the PowerShell console to indicate upload progress. One the job is done, you can browse into your storage account container to verify that the upload is completed.

Verifying that the file was uploaded to the Azure Storage Account [Image Credit: Aidan Finn]
Verifying that the file was uploaded to the Azure Storage Account [Image Credit: Aidan Finn]
Related Article: