Manage expiration of Azure Blob storage in Azure Content Delivery Network

The Blob storage service in Azure Storage is one of several Azure-based origins integrated with Azure Content Delivery Network (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 the origin server. This article describes several ways that you can set the Cache-Control header on a blob in Azure Storage.

Tip

You can 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 Overview of the Azure Content Delivery Network.

For more information about Azure Blob storage, see Introduction to Blob storage.

Azure PowerShell

Azure PowerShell is one of the quickest and 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.

For example:

# 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 Cache-Control header by using .NET, use the Azure Storage Client Library for .NET to set the CloudBlob.Properties.CacheControl property.

For example:

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 more .NET code samples available in Azure Blob Storage Samples for .NET.

Other methods

  • Azure Command-Line Interface

    When you upload a blob, you can set the cacheControl property by using the -p switch in the Azure Command-Line Interface. The following 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 settings of your blobs. With your browser's developer tools, test that your blob includes the Cache-Control response header. You can also use a tool such as wget, Postman, or Fiddler to examine the response headers.

Next Steps