Uploading and Downloading files securely from Azure Storage Blob via PowerShell

@20aman    May 18, 2016

Azure blob storage can provide a very highly available way to store your files in the cloud. You can dynamically add or remove the files in an automated fashion. These files can then be used for any number of purposes. E.g. A parameter file for ARM template can be kept in Azure blob storage and then dynamically read while creating resources from an ARM template.

The whole process can be broken down into 3 parts:

  1. Generating the context to the storage container
  2. Uploading the files using the context
  3. Downloading the files using the context

1. Generating the context to the storage container

The context to the storage blob container can be created in one of the 3 ways, based on your security requirements. All methods use the New-AzureStorageContext cmdlet to generate the storage context. The methods differ on how you pass the parameters to this cmdlet.

A. Via fetching the Azure Storage Key

This first method uses the Get-AzureStorageKey to fetch the storage key. This key is then used to generate the context as shown below.

$StorageAccountName = "yourstorageaccount"
$StorageAccountKey = Get-AzureStorageKey -StorageAccountName $StorageAccountName
$Ctx = New-AzureStorageContext $StorageAccountName -StorageAccountKey $StorageAccountKey.Primary

B. Via fetching the Azure Storage Container SAS Token

This second method uses the New-AzureStorageContainerSASToken to create a new SAS token to securely access the storage container. This token is then used to generate the context as shown below.

$sasToken = New-AzureStorageContainerSASToken -Container abc -Permission rl
$Ctx = New-AzureStorageContext -StorageAccountName $StorageAccountName -SasToken $sasToken

C. Via Connectin String

This third method uses a connection string, entered manually, which is then used to generate the context as shown below.

$ConnectionString = "DefaultEndpointsProtocol=http;BlobEndpoint=<blobEndpoint>;QueueEndpoint=<QueueEndpoint>;TableEndpoint=<TableEndpoint>;AccountName=<AccountName>;AccountKey=<AccountKey>"
$Ctx = New-AzureStorageContext -ConnectionString $ConnectionString

2. Uploading the files using the context

Now that you have the context to the storage account you can upload and download files from the storage blob container. Use the below code to upload a file named "Parameters.json", located on the local machine at "C:\Temp" directory.

#Uploading File
$BlobName = "Parameters.json"
$localFile = "C:\Temp\" + $BlobName
$ContainerName  = "vhds"

#Note the Force switch will overwrite if the file already exists in the Azure container
Set-AzureStorageBlobContent -File $localFile -Container $ContainerName -Blob $BlobName -Context $Ctx -Force

3. Downloading the files using the context

Download works in almost identical manner. You use the Get cmdlet instead of Set as shown below to download a file to a local folder, located at "C:\Downloads".

#Download File
$BlobName = "Parameters.json"
$localTargetDirectory = "C:\Downloads"
$ContainerName  = "vhds"

Get-AzureStorageBlobContent -Blob $BlobName -Container $ContainerName -Destination $localTargetDirectory -Context $ctx

I hope this helps simplify the automated usage of Azure Storage container. Let us know your concerns or questions if any.

You can find the complete sample at the below link on GitHub. Right-click and select Save As to save the file: StorageAccountBlobManagement.ps1

Reference: Using Azure PowerShell with Azure Storage





Comments powered by Disqus