REST stands for Representational State Transfer and is a style of architecture introduced by a guy named Roy Fielding (one of the main authors of HTTP). You can read about what Roy proposed at www.ics.uci.edu/~fielding/pubs/dissertation/top.htm.
Applications implementing Roy's proposed architecture are sometimes described as RESTful. I don't want to get into a debate about what exactly constitutes a RESTful system (some people who probably need to get out a bit more feel scarily passionate about this), but the important points to note are the following:
Everything is abstracted into a resource that is accessible by a unique address.
REST applications don't maintain state between requests.
These two characteristics might not seem like a big deal but are essential for cloud-based applications since they allow you to do the following:
Easily scale applications by taking advantage of features such as caching and load balancing. There is no difference at an HTTP level between a request to Azure storage and a web page request.
Write interplatform applications that integrate easily.
Everything in Azure has to be accessible using HTTP, so Azure has a number of rules regarding the naming of objects that must be adhered to (basically anything that would form a valid URL address):
Names must start with a letter or number.
Names can contain letters, numbers, and dashes only.
Every dash character must be preceded and followed by a letter.
All letters must be lowercase.
Names must be 3 to 63 characters in length.
Blobs are for storing binary data such as images, documents, and large strings. There are two types of blobs in Windows Azure: block and page blobs. Block blobs are refined for streaming operations, while page blobs are used to write to a series of bytes. A block blob can be up to 200GB in size and is uploaded in 64MB increments. Should your blob exceed 64MB, then it will be split into individual blocks, which are then reassembled. Page blobs can be up to 1TB in size.
Now you will create a program to add, delete, and display blobs. Your application will allow the user to upload images with the FileUpload control, which will then store them as a blob. You will then bind the stored blobs to a DataList to check that you have actually uploaded something.
Open Visual Studio, create a new Windows Azure Cloud Service project called Chapter16.BlobTest, and add a web role called Chapter16.BlobTestWebRole.
Open Default.aspx, and add the following code inside the <form> tag:
<asp:FileUpload ID="uploadFile" runat="server" /> <asp:Button ID="cmdUpload" runat="server" Text="Upload" /> <br /><br /> <asp:repeater ID="images" runat="server"> <ItemTemplate> <asp:Image ID="image" runat="server" ImageUrl='<%# Eval("Url") %>' /> </ItemTemplate> </asp:repeater>
Open Default.aspx.cs.
Add the following using statements:
using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.ServiceRuntime; using Microsoft.WindowsAzure.StorageClient;
Add the following code. Here, when the user uploads an image, an instance of the BlobClient is created. The BlobClient then checks whether a container called pictures exists and creates one if not. Next you create a permission object to allow everyone to view your uploaded image before saving the image. You then call the bindImages() method to display your uploaded images:
protected void Page_Load(object sender, EventArgs e) { this.cmdUpload.Click += new EventHandler(cmdUpload_Click); bindImages(); } void cmdUpload_Click(object sender, EventArgs e) { CloudStorageAccount.SetConfigurationSettingPublisher( (configName, configSetter) => { // Provide the configSetter with the initial value configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)); }); var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobClient.GetContainerReference("pictures"); blobContainer.CreateIfNotExist(); var permissions = blobContainer.GetPermissions(); permissions.PublicAccess = BlobContainerPublicAccessType.Container; blobContainer.SetPermissions(permissions); blobContainer.GetBlockBlobReference( Guid.NewGuid().ToString()).UploadFromStream(uploadFile.FileContent ); bindImages(); } public void bindImages() { CloudStorageAccount.SetConfigurationSettingPublisher( (configName, configSetter) => { // Provide the configSetter with the initial value configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)); }); var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); CloudBlobClient blobStorage = storageAccount.CreateCloudBlobClient(); CloudBlobContainer blobContainer = blobStorage.GetContainerReference("pictures"); blobContainer.CreateIfNotExist(); images.DataSource = from blob in blobContainer.ListBlobs() select new { Url = blob.Uri }; images.DataBind(); }
Add the following settings in the ServiceConfiguration.cscfg configuration block:
<Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />
Press F5 to run your project.
Click Browse, select a JPEG or GIF image, and click Upload. You should then see your picture displayed like in Figure 16-18.
If you right-click the image to examine its URL, notice how the URL consists of a number of properties you defined in your ServiceConfiguration: AccountName, pictures container, as well as the GUID you used for the ID (this URL is made up of IP:PORT/account/container/blobID) (e.g., http://127.0.0.1:10000/devstoreaccount1/pictures/4d5eee66-162e-4fb1-afcb-197f08384007).
3.145.19.7