Use Windows Azure Storage blob in Windows store to store pictures

Source: Internet
Author: User
Keywords Azure azure blob

With Windows Azure landing in China, many of the jobs that need to be done by themselves to maintain the server can be delivered to Windows Azure and more economical and convenient. In the following article, we'll take a step-by-step overview of how to use Windows Azure Storage blob in Windows Store applications to store binary large objects such as pictures, documents, and so on:

A. Install Azure Storage Client Library

Theoretically, you can use the rest API directly to access Azure Storage. But in practice this implementation requires a lot of code. To enable easy use of azure blob storage in Windows Store applications, we provide azure storage client libraries for Windows store applications. There are several ways to install a client library:

1. The client library can be downloaded from the following link:

Http://blogs.msdn.com/b/windowsazurestorage/archive/2012/11/05/windows-azure-storage-client-library-for-windows-runtime.aspx

Then, you first create a Windows Store application project using the Visual Studio 2013 template, and then add the client library by adding a reference.

2. The client library obtained in the above approach is version 2.0, and if you want to obtain the most recently published client library, you can also use the second option, using the Visual Studio 2013 template to create a Windows Store application project, and then click Tools->nuget Package manager->package Manager Console, run the following command:

Install-package Windowsazure.storage-preview–pre

Note that Azure storage client libraries can also be found in the "Project->manage NuGet Packages", but this is what is available. NET instead of Windows store, so it cannot be installed in this way.

3. Client library code is completely open source, if you want to try the latest code, you can download the source code of the client library and compile your own use:

Https://github.com/WindowsAzure/azure-storage-net

Then follow the method to add a client library.

Two. Configure Azure Storage Cloud

To use Azure storage, you first need to create a storage account on Azure that provides the information needed to access the storage service. The following are the steps required to create a storage account:

1. Log in to the Azure Management portal using Windows Azure account:

https://manage.windowsazure.com/

If you don't have a Windows Azure account, here are two ways to try out Windows Azure services for free:

A. If you want to obtain a trial account for Windows Azure China, you can go to http://www.windowsazure.cn to apply. But at present because of the number of people who apply for trial, currently only regular trial activation code, and the first out was robbed, so if you want to get, please always pay attention to their Sina Weibo @ Microsoft cloud computing.

B. If you can't wait, you can also log on to the Global Windows Azure website http://www.windowsazure.com to create a trial account. If you are unable to make a phone call or SMS verification during your account creation process, you may submit an application to the following URL to request a waiver of phone and SMS verification steps:

Https://support.microsoft.com/oas/default.aspx?prid=14238&ln=en-us&st=1&wfxredirect=1

When submitting your application, you need to set the support type as follows:

Then, in subsequent emails, tell the support staff to remove the phone and SMS verification steps.

2. After entering the Azure management portal, click Create New->storage->quick Create, enter the required URL, select the location where the Storage server resides and the backup settings, and then click Create Storage Account to create storage accounts:

3. Select the newly created storage account, click Manage Access Keys, and you can obtain the accounts name and access Key required to access the store, which will be used in our code later.

Three. Complete code to upload files from client to azure storage

After the azure cloud is configured, we can pass the file in the Windows Store application through the APIs provided by the Azure Storage client library.

First, we need to add the following using declaration:

Using Microsoft.WindowsAzure.Storage;

Using Microsoft.WindowsAzure.Storage.Blob;

Using Microsoft.WindowsAzure.Storage.Auth;

To demonstrate the file upload, we first add a button to the interface and then add the code to the button's click handler. Here we define two strings to hold the account name and access Key obtained previously, using these two strings to authenticate the accounts, and then we can build a BLOB storage client instance after the account verification. This instance is then used to access the container (Container) used to store the files in the storage, where the container is similar to the concept of a folder, and if the container does not exist, it is created automatically, and the Access property of the container is set when the container is created. The code is as follows:

var credentials = new Storagecredentials (AccountName, AccessKey);

var account = new Cloudstorageaccount (credentials, true);

var blobclient = account. Createcloudblobclient ();

var container = blobclient.getcontainerreference ("Imagecontainer");

Await container. Createifnotexistsasync ();

Await container. Setpermissionsasync (

New Blobcontainerpermissions {publicaccess = Blobcontainerpublicaccesstype.blob});

Note that the name of this container must be lowercase. Access properties are set by Publicaccess, which has three levels, and Off,blob and Container,off indicate that only an account can be used to access content in storage, that is, the content is not exposed, and the BLOB says that anonymous users can also read the contents of the container. But the information in the container itself is not disclosed. Container means that both the container and its contents are public to anonymous users.

After you have an instance of the container, you can get the block instance from the container, and then use the block instance to upload the file. The following code shows how to upload a picture named Image.jpg from a local image library to the IMAGEBLOB process:

Storagefolder Library = Windows.Storage.KnownFolders.PicturesLibrary;

var img = await library. Getfileasync ("Image.jpg");

Cloudblockblob Blockblob = container. Getblockblobreference ("Imageblob");

Await Blockblob.uploadfromfileasync (IMG);

Note that because we need to access the picture library, do not forget to select the Pictures library this capability in the configuration file Package.appxmanifest in Windows Store application.

Here we use the Uploadfromfileasync function to upload files, if your data from a data stream, you can also use the Uploadblobasync function to upload streaming data:

var stream = await img. OpenReadAsync ();

Await Blockblob.uploadfromstreamasync (stream. Getinputstreamat (0));

With the code above, we upload an image file to the Azure storage, and azure storage generates a URL for the data block:

Http://<accountName>.blob.core.windows.net/imagecontainer/imageblob

When setting the Publicaccess property, our set is blob, so this URL can be accessed anonymously by third party users, and if you do not want Third-party users to access the URL anonymously, the publicaccess attribute needs to be set to off.

Four. Complete the code to download files from Azure storage to the client

Again, we add a button to the screen for the picture download, and then add the demo code to the button's click handler. We first want to get the container that holds the picture data block:

var credentials = new Storagecredentials (AccountName, AccessKey);

var account = new Cloudstorageaccount (credentials, true);

var blobclient = account. Createcloudblobclient ();

var container = blobclient.getcontainerreference ("Imagecontainer");

A block instance is then obtained from the container instance, so that the picture file can be downloaded through a block instance:

Cloudblockblob Blockblob = container.getblockblobreference ("Imageblob");

Storagefolder Library = Windows.Storage.KnownFolders.PicturesLibrary;

var img = await library. Createfileasync ("image1.jpg");

Await Blockblob.downloadtofileasync (IMG);

Here we use the Downloadtofileasync function to download the file, and if you want to store the downloaded data into the stream, we also provide the Downloadtostreamasync function to implement the function:

Inmemoryrandomaccessstream stream = new Inmemoryrandomaccessstream ();

Await Blockblob.downloadtostreamasync (stream. Getoutputstreamat (0));

This approach applies when publicaccess is set to OFF. If the Publicaccess property of your container is set to a BLOB or container, we can access the image data directly from the URL that was obtained in the previous section:

Http://<accountName>.blob.core.windows.net/imagecontainer/imageblob

As long as you set this URL to the source of the image control, you can display the picture in your program.

In this way, we implemented the code for uploading and downloading files from the Windows Azure Storage service in Windows Store, and the complete demo code is provided in the attachment. Of course, this is just a basic demo, for a real project, we have to consider other issues, which I will explain in later articles.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.