Svenska Svenska | English English

Setup Caching on your Azure Storage Blobs

Johan Åhlén by Johan Åhlén • Updated

Azure Blob Storage can be used for publishing files to the web. Actually, this website is using Azure Blog Storage for publishing images and other resources. They each get an URL for direct access from any web browser. However, it is good practice and good for performance to setup caching on images and web resources. Here I will describe how.

By setting the Public access level on your Storage Container to "Blob", your blobs will be available to everyone through their URLs. You can link to them and use them from any website.

Azure Storage Container Access Level

Unfortunately the Azure Portal don't allow access to the caching settings of the blobs. There is no such thing in the Blob Properties shown.

Azure Storage Blob overview

Luckily, you can easily run scripts in the Azure Portal.

Using Cloud Shell for Setting the Blob Caching

Azure Cloud Shell enables you to easily run PowerShell- or Bash-script from within the Azure Portal. Open it and choose Powershell mode.

Azure Portal Cloud Shell

Next, copy the script below to a text editor. Update the settings and paste it into Cloud Shell. max-age is measured in seconds. For a complete description of the cachecontrol options, see here.


$resourcegroup = "your resource group"
$account = "your storage account"
$container = "your container"
$cachecontrol = "public,max-age=259200" # 3 days

Set-AzCurrentStorageAccount -ResourceGroupName $resourcegroup -Name $account
$blobnames = (Get-AzStorageBlob -Container $container).Name

Foreach ($blobname in $blobnames)
{
  Write-Host $blob.Name
  $blob = Get-AzStorageBlob -Container $container -Blob $blobname
  $blob.ICloudBlob.Properties.CacheControl = $cachecontrol
  $blob.ICloudBlob.SetProperties()
}

You can also read all cache settings in a similar way:


$resourcegroup = "your resource group"
$account = "your storage account"
$container = "your container"

Set-AzCurrentStorageAccount -ResourceGroupName $resourcegroup -Name $account
$blobnames = (Get-AzStorageBlob -Container $container).Name

Foreach ($blobname in $blobnames)
{
  $blob = Get-AzStorageBlob -Container $container -Blob $blobname
  Write-Host $blob.Name $blob.ICloudBlob.Properties.CacheControl
}
Testing

You should now be able to test the caching from any web browser:

Web Browser view caching

Good luck!

Azure Azure Storage

More about Azure

Running a Blog on Azure Data Lake
Azure Web App Not Updating After Publish
How To Run Your Azure Web App From Zip Package

More about Azure Storage

Running a Blog on Azure Data Lake