Add Azure Storage to an Azure Application
We previously looked at Creating an Azure App in Visual Studio and then Deploying the App on Azure . The application we built was a very simple one requiring no storage, we now turn our attention to extending the demo application to include Azure Storage. For this example we will use the blob storage in a simple Web Role.
Creating Local Blob Storage
- First we need to set up connection string to the Azure storage account. In the Cloud Service project in the Visual Studio Solution Explorer double-click the role which will use Azure storage in the Roles folder. This should bring up the configuration page for the Role. Navigate to the Settings tab and click ‘Add Setting’ , then provide a name for the setting (such as StorageConnectionString) , set the type to ConnectionString and click the ‘…’ button in the Value field. This will bring up the Storage Connection String dialog. Initially we will be developing and testing the storage on the local server so select the Use Development Storage radio button and click OK:
- In the code behind for the page which will interact with the blob storage add the following declaration to the top of the page to import the relevant namespaces (for a larger project this should be done in the web.config)
using Microsoft.WindowsAzure;
usingMicrosoft.WindowsAzure.StorageClient; - Add an instance of an Azure storage account :
CloudStorageAccount account = CloudStorageAccount.FromConfigurationSetting(“StorageConnectionString”);
- Create a CloudBlobClient object which will perform the main blob functions against the storage account. Then use the BlobClient object to create a CloudBlobContainer (all blobs must be stored in a container):
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(“hellocloud”); - Now we need to set the viewing permissions for the blob, in this example we will be setting the blob for public access so it can be viewed by anyone. This is done at the container level by creating a BlobContainerPermissions object and setting the object’s permissions to public and then passing the object to the container’s SetPermissions method.
BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
containerPermissions.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(containerPermissions); - Create an CloudBlockBlob object which will manage the individual blob and then set the URI to access the blob:
CloudBlockBlob blob;
blob = container.GetBlockBlobReference(Guid.NewGuid().ToString()); - For this example we will simply place a string in the blob (however it could be used for a file using the Upload file method of the blob object.
blob.UploadText(“AzureSupport text”)
- The blob is now available at the URI:
blob.Uri.ToString()
Move the Blob Storage to Azure online storage
- Navigate to your Windows Azure developer portal, and click “New Service” (which is located above the Windows Azure tab).
- Click Storage Account
- Enter a Service Label and click Create
- On the Create a Service – Storage Account page, enter a Public Storage Account Name and then configure the Storage Account Affinity Group (it is recommended that you create a new affinity group if there is not existing groups). Click ‘Create’ and the storage should be created.
- We will now modify the Connection String for the Visual Studio project to point to this account. To do this we need to repeat Step 1 of Create Local Blob Storage above, but enter select the Enter Storage Credentials radio button and enter the Account Name we just created and theĀ Access Key (which is the Primary Access Key shown in the Azure developer portal). You can also specify the endpoints for accessing the storage:

- Now you can deploy your application as shown in the Deploying the App on Azure tutorial. You can also enable the Azure CDN for your app.
Array




02. Feb, 2010 







No comments yet... Be the first to leave a reply!