This post details how to use the Windows Azure SDK for Node from Windows Azure Mobile Services to generate a Shared Access Signature (SAS) and then how to upload your Image (or any file) to Blob Storage directly from your Windows Store App using the Windows Azure Storage Client Library 2.0 for Windows Runtime (CTP)
Background
In my previous post How to Upload an Image using a Blob Storage SAS generated by Windows Azure Mobile Services I detailed:
- Why you should use a SAS to upload any binary data from client devices
- How you could generate your own SAS using Mobile Services Server Side scripts
- How you could use the HttpClient from a Windows Store app to upload your image using the SAS
With the recent inclusion of the Windows Azure SDK for Node in Mobile Services and the announcement of the Windows Azure Storage Client Library 2.0 for Windows Runtime (CTP) the process for performing Steps 2 and 3 are much easier. This post will detail the updated approach.
Creating your Mobile Service
In this post I will extend the Mobile Services quick start sample. Before proceeding to the next section create a mobile service and download the quickstart as detailed in the tutorial here
Capturing the Image|Media
Our first task is to capture the media we wish to upload. To do this follow the following steps.
- Add an AppBar to MainPage.xaml with a take photo button to allow us to capture the image
| 1 2 3 4 5 6 7 8 9 10 11 |
|
- Add the OnTakePhotoClick handler and use the CameraCaptureUI class for taking photo and video
| 1 2 3 4 5 6 7 8 9 10 11 |
|
- Update the TodoItem class with some properties that will be required to generate the SAS
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
- Update the OnTakePhotoClick handler to insert the todoitem setting the ContainerName and resourceName for which we want a SAS generated
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Generating a Shared Access Signature (SAS) using Mobile Services server-side script
In this step we add sever-side script to generate a SAS on insert operation of the TodoItem table.
To do this perform the following steps:
- Navigate to your Mobile Service and select the Data Tab, then click on Todoitem
- Select Script, then the Insert drop down
- Add the following server side script to create the containerName and generate a blob SAS for the resourceName
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
Using the Windows Azure Storage Client Library 2.0 for Windows Runtime (CTP) to upload the Image directly to storage using the SAS
- Download the Storage Client libraries for Windows 8 click here.
- Extract and add a reference to Microsoft.WindowsAzure.Storage.winmd your client project
- Update OnTakePhotoClick handler to update the image directly to blob storage using the CloudBlockBlob,UploadFromStreamAsync and the generated todoitem.SAS
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
Run the application
- Hit F5 on the application and right click with your mouse to show the app bar
- Press the Take Photo button
- Observe that the SAS is returned from your Mobile Service
- Check your storage account now has a great picture of a fully polished chrome dome capable of reflecting light far better then your average mirror
Enjoy,
Nick




Pingback: Windows 8 How to upload an Image using a Blob Storage SAS generated by Windows Azure Mobile Services | Nick Harris .NET
Pingback: Doporu?ené ?tení za 47. týden | MSDN Blogs
Pingback: Links of the week | Jan @ Development
Pingback: Reading Notes 2012-11-19 | Matricis
Pingback: Reading Notes 2012-11-19 - Impack Alliance