Sign in
MSDN Blogs
Microsoft Blog Images
More ...
How To Take Photographs From Windows 8 Applications And Automatically Upload Them To The Cloud–Part 4 of 6
RECENT POSTS
Fundamentals of Good Gaming – Some general guidelines – Windows 8 App Factor
Posted
27 days ago
by
BrunoTerkaly
2
Comments
Unity & Windows 8 Starter Kits - Windows 8 App Factor
Posted
29 days ago
by
BrunoTerkaly
0
Comments
Windows 8 Game Construction using Scirra’s Construct 2
Posted
30 days ago
by
BrunoTerkaly
0
Comments
Understanding XAMARIN – Create iOS, Android, Mac and Windows apps in C#.
Posted
1 month ago
by
BrunoTerkaly
2
Comments
Windows 8 and Windows Phone 8 .NET Development Overview - Windows8AppFactor
Posted
1 month ago
by
BrunoTerkaly
1
Comments
WINDOWS AZURE BLOG
RSS
http://blogs.msdn.com/b/windowsazure/archive/tags/windows+azure//
WORLD MAPS
MSDN Blogs
>
Bruno Terkaly - Developer Evangelist - bterkaly@microsoft.com
>
How To Take Photographs From Windows 8 Applications And Automatically Upload Them To The Cloud–Part 4 of 6
How To Take Photographs From Windows 8 Applications And Automatically Upload Them To The Cloud–Part 4 of 6
BrunoTerkaly
28 Aug 2012 9:26 AM
Comments
0
How To Take Photographs From Windows 8 Applications And Automatically Upload Them To The Cloud -- Part 1
http://blogs.msdn.com/b/brunoterkaly/archive/2012/08/25/how-to-take-photographs-from-windows-8-applications-and-automatically-upload-them-to-the-cloud-part-1-of-6.aspx
How To Take Photographs From Windows 8 Applications And Automatically Upload Them To The Cloud -- Part 2
http://blogs.msdn.com/b/brunoterkaly/archive/2012/08/26/tet3.aspx
How To Take Photographs From Windows 8 Applications And Automatically Upload Them To The Cloud -- Part 3
http://blogs.msdn.com/b/brunoterkaly/archive/2012/08/27/test23.aspx
How To Take Photographs From Windows 8 Applications And Automatically Upload Them To The Cloud -- Part 4
http://blogs.msdn.com/b/brunoterkaly/archive/2012/08/28/how-to-take-photographs-from-windows-8-applications-and-automatically-upload-them-to-the-cloud-part-4-of-6.aspx
How To Take Photographs From Windows 8 Applications And Automatically Upload Them To The Cloud -- Part 5
http://blogs.msdn.com/b/brunoterkaly/archive/2012/08/29/step-5-of-6.aspx
How To Take Photographs From Windows 8 Applications And Automatically Upload Them To The Cloud -- Part 6
http://blogs.msdn.com/b/brunoterkaly/archive/2012/08/29/how-to-take-photographs-from-windows-8-applications-and-automatically-upload-them-to-the-cloud-part-6-of-6.aspx
Testing Everything Locally First
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
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.
Because we will be using the storage emulation environment, Our code and Visual Studio (Web service project) We'll need to be adjusted to reflect the local storage and connection parameters.
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;
ValuesController.cs
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
// GET api/values/container/blobname
public 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;
}
}
Testing the Web service
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)
Figure A represents the Default Start Page.
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
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 above figure is the contents of values.json
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
In short, this means the
shared access signature
above can modify
brunopicture.jpg
and the container
brunophotos
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 .
Starting the Windows 8 Project, Taking a Photo, and Uploading it to the Cloud
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.
Your web service should still be running from a previous step in this post.
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 the
F5
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
.
Click anywhere on the Windows 8 application.
Drag the handles
to select the actual photo location.
Notice how serious my expression is.
Click
OK
.
Viewing the locally saved blobname using Visual Studio.
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
Viewing the photo in the cloud
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.
Viewing the photo
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.
Future Posts
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
Thanks..
I appreciate that you took the time to read this post. I look forward to your comments.
0 Comments
Leave a Comment
Name
Comment
Please add 3 and 7 and type the answer here:
Post