Setting Cache-Control on Azure Blobs can help reduce bandwidth and improve the performance by preventing consumers from having to continuously download resources.
Cache-Control allows you to specify a relative amount of time to cache data after it was received. It’s mostly recommended when you need control over how caching is done.
Usually, if we wanted to set the cache-control property of blob files, we can do such work through azure portal/windows azure storage explore… However, if you want to set the cache-control property of every file in your blob, using such ways will cost lots of time, and you may also need to change this property overtimes. A better way to do this is creating C# program using Azure Storage Client assembly and set it.
However, this blog will consider to use PowerShell script to do same work because it’s more convenient to change the property when we want. This example is to help us set storage blob cache-control Properties by PowerShell script, one thing should be noticed is we still need to use "Microsoft.WindowsAzure.StorageClient.dll" to access the blobs.
#set Properties $blobRef.Properties.CacheControl = $cacheControlValue $blobRef.SetProperties()
You need to set storage blob cache-control property / check cache-control property through PowerShell script easily.
The content of the script is reproduced below - Download and install the latest PowerShell, and connect to you subscription.
Import-AzurePublishSettingsFile -Environment AzureChinaCloud E:\PublishSettings\CIETest03-12-9-2015-credentials.publishsettings
- Set the value of Properties and Metadata
$StorageName="your storage account name"
$StorageCtx = New-AzureStorageContext -StorageAccountName $StorageName -StorageAccountKey $StorageKey
$blobs = Get-AzureStorageBlob -Container $ContainerName -Context $StorageCtx
Add-Type -Path "C:\Program Files\Microsoft SDKs\Windows Azure.NET SDK\v2.3\ref\Microsoft.WindowsAzure.StorageClient.dll"
$storageCredentials = New-Object Microsoft.WindowsAzure.StorageCredentialsAccountAndKey -ArgumentList $StorageName,$StorageKey
$blobClient = New-Object Microsoft.WindowsAzure.StorageClient.CloudBlobClient($BlobUri,$storageCredentials)
$cacheControlValue = "public, max-age=60480"
foreach ($blob in $blobs)
$blobRef = $blobClient.GetBlobReference($blob.Name)
$blobRef.Properties.CacheControl = $cacheControlValue