Logo
PREGNANCY
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
 
 
Windows Azure

Integrating BLOBs with your ASP.NET websites

3/5/2011 4:09:23 PM
In typical ASP.NET websites, you usually distribute your assets with your website. Although this strategy works great for small websites, it’s pretty much unmanageable when dealing with larger websites. Do you really want to redeploy your entire website just because you have a new Hawaiian shirt in your product range and you need to add an image of it?

There’s a better way. In this section, we’ll tell you how you can integrate public assets and private assets (such as purchased MP3 files) with your ASP.NET website.

1. Integrating ASP.NET websites with table-driven BLOB content

Currently, the shirt shop website displays a list of all the products that you have for sale, but it doesn’t display pictures of the shirts. Because the data is currently hardcoded, you could just store the image on the website directly; any changes to the product line would require you to deploy a new version of the website.

If you stored the pictures of the shirts in BLOB storage, you could store the URI of the BLOB in your external data source. As you add new items to your data source, you can add the associated BLOB at the same time. To expand on the Hawaiian Shirt Shop example, you could store an image of a shirt in BLOB storage as well as add a new shirt to the table. In the table, you would store all the details of the shirt (the name of the shirt, price, description, and so on) and its associated URI. Figure 1 shows a representation of this setup.

Figure 1. Storing URIs in an external data source

Because you’re happy for the images of your Hawaiian shirts to be in the public domain, you can store the shirt images in a public container and set the URI of your HTML image tag directly to the public URI. The following code shows how this image tag might look:

<img src="http://silverlightukstorage.blob.core.windows.net/shirtimages/greenshirt.jpg" />


Using a public container is fine for storing content that you don’t mind making available to the world, but it’s not an acceptable solution if you want to serve content that you want to keep protected.

Returning to the podcast example, suppose you decided that you wanted only paid members of your website to be able to download your MP3. In this scenario, you don’t want to store the file in a public container; you want to store it in a private container instead.

2. Integrating protected, private content

In the BLOB management web page, you implemented a download link that allowed you to serve files stored in a private container to your users on a public website. Now we’re going to expand that technique to a more integrated, reusable solution by doing the following things:

  1. Creating an HTTP handler that serves MP3 files from BLOB storage

  2. Registering the HTTP handler

  3. Protecting your handler so that only authorized users can use it

HTTP handlers let specified types of HTTP requests be handled by some custom code. In this example, you’re going to build a handler that will intercept requests for MP3 files and return the MP3 files from BLOB storage instead of from the local filesystem.

Creating the HTTP Handler

You’re going to build a small sample that will intercept any incoming requests for MP3 files using an HTTP handler. Rather than attempting to serve the MP3 files from your website’s filesystem, the handler will retrieve the files from your private BLOB storage container and serve them to the user who requested them. For this task, we’re going to build on the techniques that we used in this listing. You can either serve the files directly (useful for images) or initiate a Save dialog box. In this example, you’ll download a file directly. For example, when the user requests the following URI, they’ll be served the file from BLOB storage:

www.mypodcastwebsite.com/podcast01.mp3

To implement an HTTP handler, add a new httphandler file to your ASP.NET web project. This handler is called MP3Handler.cs. Listing 1 contains the implementation for MP3Handler.cs.

Listing 1. An MP3 HTTP handler that serves MP3s from BLOB storage

The HTTP handler in listing 9.8 will be called whenever an MP3 file request is intercepted at . When the request has been intercepted, the name of the .mp3 file that should be downloaded from BLOB storage from the requested path is extracted . The handler will then download the requested BLOB from the ChrisOriginals private container at by calling the GetBlob method . After the file is retrieved from BLOB storage, you set the MIME type as MP3 (we’ve hardcoded the MIME type but in a more generic sample you could retrieve it from the BLOB properties) and then write the file back to the client.

Registering the HTTP Handler

To register the HTTP handler with your website, add the following line to your web.config file in the handlers section under the system.webServer element:

<add name="MP3Handler" path="*.mp3" verb="GET" type="AzurePlayAreaWeb_WebRole.MP3Handler, AzurePlayAreaWeb_WebRole" resourceType="Unspecified"/>


This code will configure your server to route any web requests that end in .mp3 to your new handler. This is a great way to protect assets on your server that would normally be freely accessible. Your handler could check security permissions, route the call to virtual storage, or deny the request.

Authorization

If you want to restrict your MP3 files to logged-in users, you can use the built-in ASP.NET authorization and authentication functionality:

<authorization>
<deny users="?"/>
</authorization>

Now you have an integrated HTTP handler that can serve protected BLOB files from BLOB storage to authorized users as if it were part of your website. If your website were serving the same file to hundreds of users every day, then continually retrieving the same file from BLOB storage would be inefficient. What you would need to do in that case is use BLOB storage and local storage together, to cache requests, so you wouldn’t need to continually request the same BLOB from BLOB storage.

Shared Access Signatures

Putting BLOBs in local storage to improve performance is useful for showing you how to integrate local storage and BLOB storage together. It’s also useful as an option for integrating content. You can use Shared Access Signatures to provide the same result. Using Shared Access Signatures is the preferred approach to protect a BLOB because it’s cheaper and takes load off your servers.


Other -----------------
- 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
- The basics of BLOBs : Developing against containers (part 1) - Accessing the StorageClient library & Accessing development storage
- The basics of BLOBs : Getting started with development storage
- A closer look at the BLOB storage service
- Storing files in a scaled-out fashion is a pain in the NAS (part 2) - The BLOB service approach to file management
- Storing files in a scaled-out fashion is a pain in the NAS (part 1) - Traditional approaches to BLOB management
 
 
Most view of day
- Windows Server 2008 : Configuring Server Core after Installation (part 4) - Setting the Time, Date, and Time Zone , Joining a Domain
- Working with E-mail, Contacts, and Events : Select a Contact Address
- Microsoft Dynamics CRM 4.0 : Infrastructure Design Considerations - Windows SharePoint Integration
- Windows Server 2008 : Promoting and Demoting a Domain Controller - Using dcpromo to Install from Media, Forcing Removal of Active Directory
- Maintaining Desktop Health : Using Task Scheduler (part 1) - Task Scheduler Architecture
- Microsoft Excel 2010 : Using Formulas - Troubleshooting Formulas
- Microsoft Dynamics CRM 4 : Digital Phone Integration (part 1)
- Microsoft Visio 2010 : Sharing and Publishing Diagrams - Customizing Diagrams Saved as Websites
- Sharepoint 2013 : Service Application Administration (part 1) - Creating a New Instance of a Service Application
- Planning Deployment : Installing BDD 2007
Top 10
- Microsoft Sharepoint 2013 : Understanding app patterns (part 5) - Building MVC apps - Introducing MVC4
- Microsoft Sharepoint 2013 : Understanding app patterns (part 4) - Building MVC apps - Understanding web form challenges
- Microsoft Sharepoint 2013 : Understanding app patterns (part 3) - Building MVVM apps - Utilizing promises
- Microsoft Sharepoint 2013 : Understanding app patterns (part 3) - Building MVVM apps - Utilizing promises
- Microsoft Sharepoint 2013 : Understanding app patterns (part 2) - Building MVVM apps - Introducing knockout
- Microsoft Sharepoint 2013 : Understanding app patterns (part 1) - Building MVVM apps - Understanding JavaScript challenges
- Microsoft Access 2010 : Report Properties and Why to Use Them, Basing Reports on Stored Queries or Embedded SQL Statements
- Microsoft Access 2010 : Working with Sorting and Grouping (part 2) - Sorting and Grouping Settings
- Microsoft Access 2010 : Working with Sorting and Grouping (part 1) - Add Sorting and Grouping to a Report
- Microsoft Access 2010 : Building Reports Based on More Than One Table (part 3) - Working with Subreports
 
Windows XP
Windows Vista
Windows 7
Windows Azure
Windows Server
Windows Phone
2015 Camaro