Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone
Windows Azure Logo

Windows Azure Logo (http://www.brandsoftheworld.com/logo/windows-azure-0)

In my recent project I wanted to set the cache-control property of blob images so that they will be cached in client browser for few days which improves the performance. First I tried to use few azure storage explorer tools but it is taking lot of time to set them one by one by filtering out which files to be set. Moreover I may need to change this setting to different value in different times. Better way is to do this by prepare a text file which contains the file names of those cache-control property to be set and then run a script or program which does it for the list. Easiest way is the create C# program using Azure StorageClient assembly and set it. However I wanted to try out powershell script so that I can easily change it whenever I need. Even if I use powershell script, it will still use “Microsoft.WindowsAzure.StorageClient.dll” to access the blobs.

1. Creating Text File with File Names

This is easy, just create a file name and include names of all the files that you want to set the property (let’s name it imagelist.txt).

 2. Write the Powershell Script

Line 2: In here we just tell that we need to uses classes from the specified assembly Line 8: Create an instance of StorageCredentialsAccountAndKey class by giving azure storage name and the key as constructor parameters. Line 9: Similarly create storage account object by giving the credentials object and specifying to use https connection ($true) Line 10: This is the actual statement we need to write if we’re using C# coding, however CreateCloudBlobClient() as a extension method so we cannot call it as in C# coding. Hence as in Line 11 it has to be called as a static method. Line 13: Here we define the max-age in seconds (7 days in this example) Finally in Line 17: Read the contents of the text file “imagelist.txt” pass it to foreach loop and apply the code block to each entry in the file (accessed by $_ )

  • Create the path including the container name
  • Get reference to the blob by the container/blob name
  • Set the CacheControl property to the value we want
  • Then apply it by calling SetProperties()

Now if you want to check whether this has been applied properly you can call another powershell command by giving the absolute path to the image as follows.

Here we only request header information and then get the RawContent of the returned object. You should see something similar to below.
Hope this will help some to quickly apply blob properties using simple powershell and Azure StorageClient library.

Tweet about this on TwitterShare on FacebookShare on Google+Share on StumbleUponShare on TumblrShare on RedditPin on PinterestEmail this to someone