Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Azure

Using local storage with BLOB storage (part 2) - Updating your HTTP handler to use local storage & Checking properties of a BLOB without downloading it

3/8/2011 9:34:22 PM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

3. Updating your HTTP handler to use local storage

You’re going to modify your HTTP handler to check in local storage to determine whether the requested file exists already. If the file doesn’t exist, then you’ll retrieve the MP3 file from BLOB storage and store it in local storage. Finally, you’ll write the file back to the client from your local filesystem. The following code shows how you modify the ProcessRequest method in this listing  to use local storage.

public void ProcessRequest(HttpContext context)
{
string blobName = context.Request.Path.Trim('/');

var mp3Cache = RoleEnvironment.GetLocalResource("mp3Cache");
string localFilePath = mp3Cache.RootPath + blobName;

if (!File.Exists(localFilePath))
{
var blobData = GetBlob("chrisoriginals", blobName);
File.WriteAllBytes(localFilePath, blobData);
}

context.Response.ContentType = "audio/mpeg";
context.Response.WriteFile(localFilePath);
}

Using the local storage mechanism in your HTTP handler improves performance by serving the requested file from local storage, rather than always having to retrieve the file from BLOB storage first. Although performance is improved, if the file is changed in BLOB storage, the file that you’ll serve will be out of date. Let’s look at how you can keep the performance improvement but serve the latest content even if the file has changed in BLOB storage.

Tip

Although this sample is focused on web HTTP handlers, this technique can easily be used in worker roles. I currently use this technique with a Windows Azure MapReduce solution that I’ve built.


4. Checking properties of a BLOB without downloading it

If you were to use the HTTP HEAD verb instead of the GET verb, you could check the properties of the file without downloading the file. Figure 2 shows the output of a HEAD request.

Figure 2. The output of a HEAD request


In figure 2, the Last-Modified tag shows us the last time the file was updated in BLOB storage. By comparing the header value to the local value in your file properties, you know whether the file has changed.

x-ms-request-id

In figure 2, you’ll notice that there’s a tag called x-ms-request-id. Every request made is assigned a unique identifier (GUID) that’s returned in the response. Every request and response is logged by Microsoft; if you’re experiencing issues, you can always pass this ID with a support request—providing the ID lets Microsoft easily investigate any issues you have.


The listing that follows shows the code for the console application shown in figure 2.

Listing 1. Showing the output of a HEAD request

In the code example shown in listing 1, you make the same request that you’ve made before, but it’s a HEAD request instead of a GET request. You’re making a HEAD request for the file podcast01.mp3 in the ChrisOriginals container in your development storage account . At you loop through all the returned headers, outputting the header key and value to the console screen.

Using the Storageclient Library

In listing 1, you retrieved the last modified time using the REST API directly, although you could’ve used the StorageClient library. The following code performs a HEAD request that returns the last modified time:

blob.FetchAttributes();
var lastModifiedTime = blob.Properties.LastModifiedUtc;

FetchAttributes is the StorageClient library equivalent of HEAD. It returns all the properties and custom metadata of the BLOB, without downloading the actual file. Table 1 lists the BLOB properties and their descriptions.

Table 1. BLOB properties
Property nameDescription
BlobTypePageBlob, BlockBlob
CacheControlAllows you to instruct the browser on how to cache the BLOB
ContentEncodingEncoding of the header
ContentLanguageLanguage header of the BLOB
ContentMD5MD5 hash of the content header
ContentTypeThe MIME type of the BLOB
EtagUnique identifier of the request (changes every time a BLOB is modified)
LastModifiedTimeUtcLast time the BLOB was modified
LeaseStatusUsed with page blocks: Locked, Unlocked; not available in the StorageClient library.
LengthSize of the BLOB, in bytes

Now that you’re familiar with BLOB properties and how to retrieve them without downloading the file, let’s look at how you can use this knowledge to improve the performance of your handler.

Other -----------------
- Using local storage with BLOB storage (part 1) - Using a local cache & Defining and accessing local storage
- Integrating BLOBs with your ASP.NET websites
- Downloading BLOBs
- Managing BLOBs using the StorageClient library (part 2) - Uploading BLOBs & Deleting BLOBs
- Managing BLOBs using the StorageClient library (part 1) - Listing BLOBs using the storage client
- Using the REST API (part 2) - Authenticating private requests
- Using the REST API (part 1) - Listing BLOBs in a public container using REST
- The basics of BLOBs - Configuring your application to work against the live service
- The basics of BLOBs : Developing against containers (part 3) - Listing containers & Deleting a container
- The basics of BLOBs : Developing against containers (part 2) - Creating a container
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server