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

Upgrade Azure Active Directory Connect fails with unexpected error

Today, I made the decision to upgrade my test environment and update the version of…

1 miesiąc ago

Mastering Active Directory Hygiene: Automating Stale Computer Cleanup with CleanupMonster

Have you ever looked at your Active Directory and wondered, "Why do I still have…

2 miesiące ago

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…

7 miesięcy 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…

11 miesięcy 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…

1 rok 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…

1 rok ago