Scroll Top
Evotec Services sp. z o.o., ul. Drozdów 6, Mikołów, 43-190, Poland

Upload and Download files from Azure Blob Storage using Connection String

Azure Blob Storage List

They say there is a first time for everything. For me, it's how to download and upload files to Azure Blog Storage using Connection String. Recently I was given Connection String, Container name and had to download some files from Azur Blog Storage. After some research and trying Connect-AzAccount, I found that the proper way to go is thru New-AzStorageContext.

Uploading / Downloading files Azure Blog Storage

The first thing we need to do is to install Az.Storage module, which in turn will also download Az.Accounts module.

Install-Module -Name 'Az.Storage' -Force -Verbose -Scope CurrentUser

I always like to use Force and Verbose parameters. Force makes sure it redownloads everything that is required and makes sure it's up to date. Verbose adds a bit more information about what is happening. Install-Module tends to go slow, and things can look like nothing is happening. Once we have our modules installed, we need to define ConnectionString and ContainerName and use New-AzStorageContext. We then pass the Context variable to Get-AzStorageBlob to get a list of available files on the storage.

# define data
$DestinationFolder = "$PSScriptRoot\Downloads"
$ContainerName = "eclcontainer"
$ConnectionString = "DefaultEndpointsProtocol=https;AccountName=test;AccountKey=rrandomKeymXwY8/+dPIHH8=;EndpointSuffix=core.windows.net"

# connect to blob storage
$Context = New-AzStorageContext -ConnectionString $ConnectionString

# list current content
$ContainerStorage = Get-AzStorageBlob -Container $ContainerName -Context $Context
$ContainerStorage | Format-Table

It's important to know that the blob storage is case-sensitive. That means you can upload multiple files with the same name but different casing. Downloading all available files is available by using foreach and Get-AzStorageBlobContent.

# go thru each file and download
foreach ($File in $ContainerStorage) {
    $Destination = "$DestinationFolder\$($File.Name)"
    Get-AzStorageBlobContent -Container $ContainerName -Blob $File.Name -Context $Context -Destination $Destination -Force
}

If you want to overwrite existing content, you need to use the Force switch. Otherwise, you will be prompted for every file on the disk. To upload the file, you have to use Set-AzStorageBlobContent. You can upload a file with the same name as it has on the drive by not providing the Blob parameter or providing it and giving it a different name on the storage.

# upload file to blob storage
$SourceFile = "$PSScriptRoot\Downloads\test.PDF"
# upload file to blob storage but without changing the name
Set-AzStorageBlobContent -File $SourceFile -Container $ContainerName -Context $Context
# upload file to blob storage and change the name
$DestinationFile = "test1.pdf"
Set-AzStorageBlobContent -File $SourceFile -Container $ContainerName -Context $Context -Blob $DestinationFile

And that's it. You now know how to upload and download files from Azure Blob Storage.

Related Posts