This tutorial demonstrates how to use the Windows Azure Blob Service.
In [[Windows Azure and SQL DatabaseTutorials - Tutorial 1: Using Windows Azure Web Role and Windows Azure Table Service|tutorial 1]], you created a simple golfer message board application. In the application, a Web role provides the front-end that allows golfers to view the contents of the message board and add new entries. Each entry contains a name and a message. When golfers post a new message, the Web role creates an entry using the Table service that contains the information entered by the golfers. The Web role also renders this information to the browser so golfers can view the content of the message board.
Tutorial 3 is based on tutorial 1. In this tutorial, you will modify the application you created in tutorial 1. In addition to golfer name and a short message for each message ported to the message board, you will also add an image. You will keep using the Table service for the text portion of the messages, but use the Blob service for images.
Last reviewed: 11/4/2011
Note: Completing tutorial 1 is not a pre-requisite for this tutorial. However, it helps with understanding the scenario.
Note: The tutorial code has been tested on Windows Azure SDK (October 2012).
In this tutorial, you will learn how to:
Note the following requirements before you begin this lesson:
The following diagram illustrates the development components and the runtime components involved in this tutorial:
The Windows Azure Blob service enables applications to store large objects, up to 1TB each. It supports a massively scalable blob system via the Windows Azure CDN, where hot blobs will be served from many servers to scale out and meet the traffic needs of your application. Furthermore, the system is highly available and durable. You can always access your data from anywhere at any time, and the data is replicated at least 3 times for durability. In addition, strong consistency is provided to ensure that the object is immediately accessible once it is added or updated; a subsequent read will immediately see the changes made from a previously committed write.
An application must use a valid account to access Windows Azure Storage. An account can have many Blob containers. Each container provides a grouping of a set of blobs.
In this lesson, you modify the golfer message board application to use the Blob service for storing images.
In this lesson, you will go through the following procedures:
To open the Golfer Message Board application
In [[Windows Azure and SQL DatabaseTutorials - Tutorial 1: Using Windows Azure Web Role and Windows Azure Table Service|tutorial 1]], you defined a fixed schema for the Table serivce to storage messages. You must modify the schema to include a new data member for storing the URL to the image in the Blob service.
To modify the table schema
public string ImageURL {get; set;}
The file looks like the following after modification:
Modify the data source
using System.IO
private const string messageImageBlobName = "golfermessageboardpics"; private CloudBlobClient blobClient; private CloudBlobContainer blobContainer;
Note: All letters in a container name must be lowercase. For more restrictions on container names, see Naming Containers, Blobs, and Metadata.
blobClient = storageAccount.CreateCloudBlobClient(); blobContainer = blobClient.GetContainerReference(messageImageBlobName); blobContainer.CreateIfNotExist(); var permissions = blobContainer.GetPermissions(); permissions.PublicAccess = BlobContainerPublicAccessType.Container; blobContainer.SetPermissions(permissions);
public string AddBlob(string fileExtension, string fileContentType, Stream fileContent) { // upload the image to blob storage string uniqueBlobName = string.Format(messageImageBlobName + "/image_{0}{1}", Guid.NewGuid().ToString(), fileExtension); CloudBlockBlob blob = blobClient.GetBlockBlobReference(uniqueBlobName); blob.Properties.ContentType = fileContentType ; blob.UploadFromStream(fileContent ); return blob.Uri.ToString(); }
Default.aspx presents the message board Web interface. You must modify the page so that golfers can upload an image, and the page can display images along with messages. See the screenshot at the beginning of the tutorial.
To modify the default.aspx file
<dt> <label for="FileUpload1">Photo:</label> </dt> <dd> <asp:FileUpload ID="FileUpload1" runat="server" size="16" /> <asp:RequiredFieldValidator ID="PhotoRequiredValidator" runat="server" ControlToValidate="FileUpload1" Text="*" /> <asp:RegularExpressionValidator ID="PhotoRegularExpressionValidator" runat="server" ControlToValidate="FileUpload1" ErrorMessage="Only .jpg or .png files are allowed" ValidationExpression="([a-zA-Z\\].*(.jpg|.JPG|.png|.PNG)$)" /> </dd>
If you don't see the code snippet, follow the instructions in Windows Azure and SQL Database Tutorials (en-US) to copy the code snippets.
The <div> tag looks like the following after the modification:
<div class="signatureImage"> <img src="<%# Eval("ImageUrl") %>" alt="<%# Eval("GolferName") %>" /> </div>
Next, you modify the default.aspx.cs for creating and using the Blob service. In the code, you define a lock to prevent the Blob service being created multiple times.
To modify the default.aspx.cs file
using System.IO;
In this step, you introduced the Blob serivce to the golfer message board application.
You will test the application and prepare the service package for the application.
Return to Top
In this lesson, you test the application in the compute emulator environment, package the application, and then deploy the application to Windows Azure.
To test the application
After the application is tested successfully in the compute emulator environment, the next step is to create the service package and then deploy the application to Windows Azure. To generate the service package
You will get a few warning messages about 'DataConnectionString" set up to use the local storage emulator. You can ignore these warning for now.
For deploying the golfer message board application, you must have a storage account for accessing the Windows Azure storage services, and a cloud service, which is a container for service deployments in Windows Azure. For better performance, you might want to create an affinity group to group the service and the storage accounts within a subscription according to geo-location.
To sign in to Windows Azure
Note: If you haven’t had a Windows Azure Platform subscription, see the Provisioning Windows Azure section of this tutorial.
The next step is to create a storage account for using Windows Azure table service and blob service. If you have created a storage account in [[Windows Azure and SQL DatabaseTutorials - Tutorial 1: Using Windows Azure Web Role and Windows Azure Table Service|tutorial 1]], you can skip this step.
To create a storage account for the golfer message board application to store its data
If you have published other tutorial projects before, you can either update the existing cloud service, or create a new cloud service. If you choose to create a new cloud service, you must give a different cloud service URL.
To create a cloud service
Note: The URL prefix must be unique.
When you create and test the application locally, the application is configured to use the development storage. Now you have created a storage account, you can configure the application to use the storage account before deploying the application to Windows Azure. The configuration information is in the ServiceConfiguration.Cloud.cscfg file. This file was created when you generated the service package.
To configure the ServiceConfiguration.Cloud.cscfg file
To deploy the application to the staging environment
To test the application in the staging environment
After the application is working correctly in the staging environment, you are ready to promote it to the production environment.
To promote the application to production
Note: Some DNS services take longer to replicate the records. If you get a page not found error, you might need to try browsing to the URL again in a few minutes.
In this step, you tested and deployed the golfer message board to Windows Azure.
Congratulations! You have completed tutorial 3. [[Windows Azure and SQL Database Tutorials - Tutorial 4: Using Windows Azure Worker Role and Windows Azure Queue Service|Tutorial 4]] shows you how to use another Windows Azure storage called Windows Azure Queue service.
Maheshkumar S Tiwari edited Revision 45. Comment: Added Tag
Carsten Siemens edited Revision 44. Comment: Fixed misspellings
Richard Mueller edited Revision 43. Comment: Replace RGB values with color names in HTML to restore colors
Richard Mueller edited Revision 42. Comment: Removed (en-US) from title, added tags
Carsten Siemens edited Revision 41. Comment: fixed typo
Jonathan Gao edited Revision 37. Comment: fix links
Jonathan Gao edited Revision 36. Comment: Update title and SQL Database name
Jonathan Gao edited Revision 31. Comment: update the architecture diagram
Jonathan Gao edited Revision 23. Comment: updating tags
PenniJ edited Revision 22. Comment: Fixed broken link.
Jonathan Gao edited Original. Comment: minor updates
PenniJ edited Revision 4. Comment: Minor edits.
Jonathan Gao edited Revision 5. Comment: add a link to the tutorial landing page
Jonathan Gao edited Revision 6. Comment: rename the article