Add Cache-Control and Expires headers to Azure Storage Blobs

前端 未结 8 1739
刺人心
刺人心 2020-12-04 21:48

I\'m using Azure Storage to serve up static file blobs but I\'d like to add a Cache-Control and Expires header to the files/blobs when served up to reduce bandwidth costs.

8条回答
  •  广开言路
    2020-12-04 22:08

    This might be too late to answer, but recently I wanted to do the same in different manner, where I have list of images and needed to apply using powershell script (of course with the help of Azure storage assembly) Hope someone will find this useful in future.

    Complete explanation given in Set Azure blob cache-control using powershell script

    Add-Type -Path "C:\Program Files\Microsoft SDKs\Windows Azure\.NET SDK\v2.3\ref\Microsoft.WindowsAzure.StorageClient.dll"
    
    $accountName = "[azureaccountname]"
    $accountKey = "[azureaccountkey]"
    $blobContainerName = "images"
    
    $storageCredentials = New-Object Microsoft.WindowsAzure.StorageCredentialsAccountAndKey -ArgumentList $accountName,$accountKey
    $storageAccount = New-Object Microsoft.WindowsAzure.CloudStorageAccount -ArgumentList $storageCredentials,$true
    #$blobClient = $storageAccount.CreateCloudBlobClient()
    $blobClient =  [Microsoft.WindowsAzure.StorageClient.CloudStorageAccountStorageClientExtensions]::CreateCloudBlobClient($storageAccount)
    
    $cacheControlValue = "public, max-age=604800"
    
    echo "Setting cache control: $cacheControlValue"
    
    Get-Content "imagelist.txt" | foreach {     
        $blobName = "$blobContainerName/$_".Trim()
        echo $blobName
        $blob = $blobClient.GetBlobReference($blobName)
        $blob.Properties.CacheControl = $cacheControlValue
        $blob.SetProperties()
    }
    

提交回复
热议问题