Manage expiration of Azure Storage blobs in Azure CDN

The blob service in Azure Storage is one of several Azure-based origins integrated with Azure CDN. Any publicly accessible blob content can be cached in Azure CDN until its time-to-live (TTL) elapses. The TTL is determined by the Cache-Control header in the HTTP response from Azure Storage.

Tip

You may choose to set no TTL on a blob. In this case, Azure CDN automatically applies a default TTL of seven days.

For more information about how Azure CDN works to speed up access to blobs and other files, see the Azure CDN Overview.

For more details on the Azure Storage blob service, see Blob Service Concepts.

This tutorial demonstrates several ways that you can set the TTL on a blob in Azure Storage.

Azure PowerShell

Azure PowerShell is one of the quickest, most powerful ways to administer your Azure services. Use the Get-AzureStorageBlob cmdlet to get a reference to the blob, then set the .ICloudBlob.Properties.CacheControl property.

# Create a storage context
$context = New-AzureStorageContext -StorageAccountName "<storage account name>" -StorageAccountKey "<storage account key>"

# Get a reference to the blob
$blob = Get-AzureStorageBlob -Context $context -Container "<container name>" -Blob "<blob name>"

# Set the CacheControl property to expire in 1 hour (3600 seconds)
$blob.ICloudBlob.Properties.CacheControl = "public, max-age=3600"

# Send the update to the cloud
$blob.ICloudBlob.SetProperties()
Tip

You can also use PowerShell to manage your CDN profiles and endpoints.

Azure Storage Client Library for .NET

To set a blob's TTL using .NET, use the Azure Storage Client Library for .NET to set the CloudBlob.Properties.CacheControl property.

class Program
{
    const string connectionString = "<storage connection string>";
    static void Main()
    {
        // Retrieve storage account information from connection string
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);

        // Create a blob client for interacting with the blob service.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Create a reference to the container
        CloudBlobContainer container = blobClient.GetContainerReference("<container name>");

        // Create a reference to the blob
        CloudBlob blob = container.GetBlobReference("<blob name>");

        // Set the CacheControl property to expire in 1 hour (3600 seconds)
        blob.Properties.CacheControl = "public, max-age=3600";

        // Update the blob's properties in the cloud
        blob.SetProperties();
    }
}
Tip

There are many more .NET code samples available in the Azure Blob Storage Samples for .NET.

Other methods

  • Azure Command-Line Interface

    When uploading the blob, set the cacheControl property using the -p switch. This example sets the TTL to one hour (3600 seconds).

    azure storage blob upload -c <connectionstring> -p cacheControl="public, max-age=3600" .\test.txt myContainer test.txt
    
  • Azure Storage Services REST API

    Explicitly set the x-ms-blob-cache-control property on a Put Blob, Put Block List, or Set Blob Properties request.

  • Third-party storage management tools

    Some third-party Azure Storage management tools allow you to set the CacheControl property on blobs.

Testing the Cache-Control header

You can easily verify the TTL of your blobs. Using your browser's developer tools, test that your blob is including the Cache-Control response header. You can also use a tool like wget, Postman, or Fiddler to examine the response headers.

Next Steps