Update Azure Storage Blob Properties with PowerShell
In the past days I uploaded a lot of files to an Azure Blob Storage using the Azure Storage Explorer.
Unfortunately, I forgot to specify system properties such as cache content when uploading to Azure Storage Explorer. Currently the only option here is the Azure CLI and the "upload-batch" command. The files were already uploaded in the meantime, so I didn't want to do the upload again.
Unfortunately, there is currently no command in the Azure Storage Explorer and the Azure CLI to update system properties such as cache content with a one-liner.
So I wrote myself a PowerShell script that reads all the blob storage items, filters for file extensions and updates the cache content.
# Storage Settings
$storageAccount = "__storage account__";
$containerName = "__container name__";
# Blob Update Settings
$contentCacheControl = "public, max-age=2592000"; # 30 days
$extensions = @(".gif", ".jpg", ".jpeg", ".ico", ".png", ".css", ".js");
# Read all blobs
$blobs = az storage blob list --account-name $storageAccount --container-name $containerName --num-results * --output json | ConvertFrom-Json
# iterate all blobs
foreach($blob in $blobs)
{
# use name as identifier
$blobName = $blob.name;
# get extension
$extension = [System.IO.Path]::GetExtension($blobName).ToLower();
# update blob if extension is affected
if($extensions.Contains($extension))
{
az storage blob update --account-name $storageAccount --container-name $containerName --name $blobName --content-cache-control $contentCacheControl
Write-Host "Updated $blobName"
}
}
Azure CLI - Upload Batch
During my research I also found the upload-batch
command, with which I could have specified this directly during the upload.
az storage blob upload-batch --account-name $storageAccount --destination $containerName --source C:\your\local\folder --content-cache-control "public, max-age=2592000"