This is an additional post for Migrating ASP.NET Application to Windows Azure series. Please refer to previous related posts if you have not read it before.
In the step 6 of the Part 3 post, remembering that I commented some portion of the code in AdminPhoto.aspx and App_CodePhotoManager.cs which unable be run directly on Windows Azure. What actually the feature does is enable us to perform bulk copy from the web server’s folder namely “upload” to the database. However, those code could not simply work on Azure since we have no control over the “upload” folder when our app has been uploaded to Windows Azure.
The idea to implement similar feature
As I promise to answer to this issue, in this post I’ll show you step-by-step how to implement similar feature. We will be do those things in 2 files: AdminPhoto.aspx and AdminPhoto.aspx.cs.
The idea to use blob storage as replacement of “upload” folder. We can upload our photos to blob storage by using tool such as Azure Storage Explorer.
Assuming that I’ve created a container called “photos” and uploaded some photos in that container.
When user is ready to upload the photos inside the container, they will click on Import and what our application do is to store these photos to SQL Azure using the existing class library “PhotoManager.cs”.
*Please note that actually the better practice is to store those file in blob storage rather than SQL Azure since scalability and cost-efficient concern. However, since I’ve used SQL Azure to store the data (including photos) from the beginning of this post, I’ll just continue to do with respect to consistency manner.
Step by Step: how to implement
1. DataConnectionString setting on compute role.
Since we are using Azure Storage, we’ll need to add a setting on intended role (in my sample: PersonalWebRole) as shown below.
To do that, double click on PersonalWebRole which is located below CloudServicePersonalRoles folder. Subsequently click on Settings tab.
You may note that in the example I use DevelopmentStorage, of course you could be able to use the “real” Azure Storage. All you need to do is to create a Azure Storage Service and then specify the service name as well as the key as the value.
2. Some codes in WebRole.cs
To tell WebRole that “please refer to the setting to retrieve the storage value”, we’ll need to add something on OnStart method inside WebRole.cs. (the yellow highlighted section).
3. In AdminPhoto.aspx file.
I replace the commented section with the following codes.
Let me explain you how these things were added
a. I added a DropDownList called ddlBlobContainer. This ddlBlobContainer will be used to list out all of our blob containers that were associated with our storage account.
b. When we select on one of the ddlBlobContainer, we’ll fetch all blob item in that container and display it to user interface (UI). l use ListView as UI container rather than of using DataList. Inside the ListView, I added Image object to show the thumbnail display, so that use could be able to see it before performing bulk copy.
c. I use UpdatePanel to enable ajax functionality so that the browser won’t be refreshed in full page view. (Remember that we’ll also need to include the ScriptManager in order to use UpdatePanel).
4. In AdminPhoto.aspx.cs file.
a. The next thing to do when using Azure Storage is to initialize the storage account as well as object that we are going to use.
- The storageInitialized variable is used as flag to indicate whether the storage has been initialized
- The gate variable is to be used along with “lock” statement, to ensure that other threads won’t enter the critical section before the resource has been released.
- The blobStorage object serves as CloudBlobClient, to manipulate the blob of container.
- In the InitializeStorage method, we get the connection value from the settings, create cloud blob client, get / create the specific container (In this example called “photos”) and finally set the permission of the container to public access.
b. Subsequently, I added BindUploadList() method which fetches blob from the container and display it to the listview.
I also ensure that only file that ends with .jpg, .png, gif will be displayed.
c. The next thing is to implement the UploadList_ItemDataBound method to handle the ItemDataBound event.
d. To ensure that each time the photos would be refreshed when container was selected, I implemented the following snippet.
e. In the Page_Load method, I get the setting first and then dump the blob containers to the dropdownlist, so that user could select the available container.
f. When the button “Import” was clicked, I loop through all the available photo in the container. For each blob item in the container, I’ll read the blob item, buffer it into array of byte, and finally submit the photos to the SQL Azure.
Let’s press Run button and see how it works!
a. We’ll need to login as administrator first before we can perform bulk upload.
b. Having done so, please proceed to Manage link and click Edit on one of the Album.
c. You can now see the list of container in the dropdownlist, once you select one of them, the photos are refreshed accordingly.
Now, click on the Import button. In few moments, you can see that, you’ve successfully uploaded the photos.
Alright, we are now done.
I hope you enjoy this post and would be useful for you. See you at another post.