How to: Take Photos and Auto Upload them to the Cloud pt. 4-6
- We are now ready to begin some testing.
- Now would be a good time to connect your web cam.
- The great news is that the Azure SDK and tool and introduces two emulators.
- The first and later will let us emulate the web service running on our local machine.
- This means we do not need to deploy it to a data center to test it.
- The other emulator is the storage emulator.
- The storage emulator will allow us to save blobs locally, without requiring us to use a storage account of a data center.
- The system uses SQL server to emulate blob storage.
- This is all transparent to you so you do not need to worry about it.
- We will first run the web service.
- After running the web service we can test it with a simple browser.
- The browser is a great way to test your RESTful Web Service.
- The next step would be to test it with the Windows 8 application .
- The Windows 8 application can also use the storage emulator to upload the photograph as a blob.
- We can then use the built in tools of Visual Studio to inspect our pictures uploaded as blobs on our local machine
- The Windows 8 application can also use the storage emulator to upload the photograph as a blob.
- When everything works as we expected, the next step is to try to deploy our web service to the cloud.
- We will also need to modify the web service to reflect the storage account that we will provision at up the portal.
- Later, when we go to the portal to provision our storage account, we will once again modify the code of the web service, to reflect the storage account hosted in a data center. We wish to use the storage emulator and that is what the code above reflects.
- This is the crucial line of code that tells the system to use the local development storage account emulation environment :
- CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
?
| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 | // GET api/values/container/blobnamepublic string Get(string container, string blobname){ try { // Make sure this line is commented out. //CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("DataConnectionString")); // This tells our web service to use the storage emulator.If CloudStorageAccount storageAccount = CloudStorageAccount.DevelopmentStorageAccount; // Client object provides a client for accessing the Windows Azure Blob service. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // All blobs are written into a container CloudBlobContainer blobContainer = blobClient.GetContainerReference(container); // Create container if does not exist. blobContainer.CreateIfNotExist(); // Mark the container of the image as public so that can be read by anyone. BlobContainerPermissions containerPermissions = new BlobContainerPermissions(); containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; // Define a 4 hour window that the Windows 8 client can write to Azure Blob Storage. containerPermissions.SharedAccessPolicies.Add("mypolicy", new SharedAccessPolicy() { Permissions = SharedAccessPermissions.Write, // | SharedAccessPermissions.Read , //To be available immediately don't set SharedAccessStartTime = SharedAccessExpiryTime = DateTime.Now.Add(TimeSpan.FromHours(4)) }); // Set the permissions so that Windows 8 client can write to the container // for the 4 hours specified above. blobContainer.SetPermissions(containerPermissions); // Create the shared access signature that will be added to the URL. string sas = blobContainer.GetSharedAccessSignature(new SharedAccessPolicy(), "mypolicy"); // Creat the URI to be return to the Windows 8 client that will be used to write // to blob storage. return string.Format("{0}/{1}{2}", blobContainer.Uri, blobname, sas); } catch (Exception ex) { // Return error message to client. string error = ex.Message; return error; } } |
- Recall that there are two projects.
- Return to the WebService project. Figure 1 above.
- Perform the following:
- From the Debug menu choose Start Debugging.
- You should see figure 2 above.
- It shows the emulators starting up.
- Two emulators
- Storage (Blob)
- Compute (Web Service)
- You are looking at the startup screen for our ASP.NET Web API application.
- But the real goal of this section is to call into our custom get method that we just added.
- This get(string container, string blobname) method will return a shared access signature
- As you recall this method will return a shared access signature, given a container name and blob name .
- We will type the following URL into the address bar of the browser :
- http://127.0.0.1:81/api/values?container=brunophotos&blobName=brunopicture.jpg
- We will save that file. It is called values.json.
- Next, we will open up the file to view its contents.
- What we expect to see is a shared access signature that was created and sent by the web service.
- This is a good way to test our web service with a browser, before actually using the Windows 8 application to do the same thing.
- As you saw from the previous section, values.json should contain assured access signature created by the web service as a result of passing in the container and a blob name.
- The first parameter is container
- The value passed and was brunophotos
- The second parameter is blobname
- The value passed was brunopicture.jpg
- The first parameter is container
- This approach is a great way to verify the web service running correctly
- Once we verify that the URL works correctly, we are ready to start testing a similar call from a Windows 8 application.
- The URL you see in notepad reflects the contents of a shared access signature that was created by the web service.
- Any client with the http capabilities can retrieve a shared access signature as you see above
- Any client in posession of a shared access signature has the ability to write and manipulate the picture and container originally passed in.
- The first parameter is container
- The value passed and was brunophotos
- The second parameter is blobname
- The value passed was brunopicture.jpg
- The first parameter is container
- In short, this means the shared access signature above can modify brunopicture.jpg and the containerbrunophotos For the four hour window of time specified in the web service code.
- We generated the shared access signature you see above with some simple boilerplate code.
- You can reuse the same code in your own applications .
- Notice that the URL and figure a matches the URL we just use with the browser.
- Further notice that this URL is working with the Local emulation environment installed with the Azure SDK.
- Notice the address of http://127.0.0.1:81/
- This is the local emulation environment.
- _photoSAS We'll be modified to include a container and a blobname.
- The blobname will essentially be the photo that this captured using the camera API
- We will once again modify _photoSAS once we have created a true storage account using the windows azure portal.
- We are ready to run the Windows 8 Project.
- Before running this application let's make sure that it will build correctly.
- From the Build menu, select Build Solution. See Figure A
- Assuming there are no errors, you are ready to run. From the Debug menu, select Start Debugging or simply hit theF5 key. See figure B.
- You should see 0 failed.
- Once the Windows 8 application starts up, you should see figure C, at which point you should click on the Capture Photo button.
- Drag the handles to select the actual photo location.
- Notice how serious my expression is.
- Click OK.
- In a future post, once we create a real storage account, We will be able to view the blob from anywhere with an Internet connection.
- But for now we're just testing the local environment.
- We will be able to test many things:
- We can verify that the web service will return a shared access signature that permits us from the Windows 8 application to create a container with a viewable picture .
- We can verify that the Windows 8 application saves the photograph locally and then uploads it as a stream of bytes to the Storage Service using the shared access signature provided by the web service
- The photo has been uploaded to the emulated cloud. It is ready to view.
- Because we are running in the local emulation environment, the photo has been loaded on your own computer.
- If we had deployed the web service to the cloud and had pointed our Windows 8 application to the cloud, the photo would be in the cloud.
- The photo would then be available to anyone connected to the internet.
- To view the photo, you can use the built-in tooling in Visual Studio.
- Return to Visual Studio where we built the Web Service Project.
- From the View menu, select Server Explorer.
- Navigate to Windows Azure Storage/(Development)/Blobs/photocontainer
- You will see your photo in the container. In my case the photo is picture035.jpg.
- Right mouse click as seen in the figure above.
- Notice the url pointing to the blog is http://127.0.0.1:1000/devstoreaccount1/photocontainer/picture035.jpg
- The above url is almost the publicly available url that clients will use to view the uploaded photo.
- But because we are running locally, this won't be true.
- Once we use a REAL storage account, we will be able to show the photo to anyone with an internet connection.
- In the previous section we noticed that the url is pointing to the blob is:
- http://127.0.0.1:1000/devstoreaccount1/photocontainer/picture035.jpg
- We can now start Internet explorer and browse to that location using the URL above.
- Next Steps
- Deploying the Web Service to a Microsoft Data Center
- Adjusting the Windows 8 Client app to talk to our hosted service
- Download the free trial
- You will need a trial account for Windows Azure
- Please sign up for it here:
- http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200114759
- Please sign up for it here:
- You will need a trial account for Windows Azure
I appreciate that you took the time to read this post. I look forward to your comments.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)
Tags:




