# 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.

blog

18 Jan 2023 fr

![Upload and Download files from Azure Blob Storage using Connection String](/wp-content/uploads/2023/01/img_63c8357c4208e.png)

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.

```powershell
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.

[!\[PowerShell table listing Azure Storage blobs with blob type, length, content type, last modified time, access tier, and deletion status.\](/wp-content/uploads/2023/01/img_63c8357c4208e.png)](/wp-content/uploads/2023/01/img_63c8357c4208e.png)

```powershell
# 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**.

```powershell
# 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.

```powershell
# 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**.

## Projets associés

Projets liés aux tags, catégories et au contenu de cet article.

[Tous les projets](/fr/projets/)

Recherche des projets associés...

[Retour au blog](/blog)

## Articles associés

- [Active Directory Health Check using Microsoft Entra Connect Health Service](/fr/blog/active-directory-health-check-using-microsoft-entra-connect-health-service/)
- [Report Active Directory Accounts that are Synchronized with Azure AD](/fr/blog/report-active-directory-accounts-that-are-synchronized-with-azure-ad/)
- [Office 365 – Limiting license to minimum apps required](/fr/blog/office-365-limiting-license-to-minimum-apps-required/)

<!-- PowerForge markdown artifact sha256:35F66941E8A0F44B7D3660973815F3AF5D8CDC4E7BF37EAD3EB3171CA68BE22B -->
