Thursday, November 14, 2019

How to download the contents in Azure Blob storage using Powershell?

In this blog, I am going to discuss about how we can download the files inside our blob storage using powershell. For basic understanding about blob storage, you can refer to this link:https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blobs-introduction

For this you need to have azure cli installed in your machine as a prerequisite: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-windows?view=azure-cli-latest

Now in power-shell run the script as below:

#Note the Container name from storage explorer
$container_name = 'packer-test'


#Enter path where you need to download the files from blob
$destination_path = 'C:\Path'

#Copy the connection string of the storage account where blob exists:
#To get the connection string go to Storage account and check for Access Keys

# For more details on connection string, refer: https://docs.microsoft.com/en-us/azure/storage/common/storage-configure-connection-string


$connection_string = 'xxxxxxxxxxxxxxxxxxxxxxxxw==;EndpointSuffix=core.windows.net'

$storage_account = New-AzureStorageContext -ConnectionString $connection_string

# Get the blobs list
$blobs = Get-AzureStorageBlob -Container $container_name -Context $storage_account

# Just download the blob which is required. Here I, have mentioned "myfiletodownload.txt"

Get-AzureStorageBlobContent `
    -Container $container_name -Blob "myfiletodownload.txt" -Destination $destination_path `
    -Context $storage_account

Now if you check in your destination path, you will find the myfiletodownload.txt(blob content) downloaded.

No comments:

Post a Comment