Azure AD

Upload and Download files from Azure Blob Storage using Connection String

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.

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

2 days ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

5 months ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

6 months ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

8 months ago

How to Efficiently Remove Comments from Your PowerShell Script

As part of my daily development, I create lots of code that I subsequently comment…

8 months ago

Unlocking PowerShell Magic: Different Approach to Creating ‘Empty’ PSCustomObjects

Today I saw an article from Christian Ritter, "PowerShell: Creating an "empty" PSCustomObject" on X…

8 months ago