Windows azure platform (22) Storage Service of Windows azure storage service blob (I) Windows azure platform series articles

Source: Internet
Author: User
Tags configuration settings visual studio 2010

Windows azure platform articles

 

This chapter describes how to use blob storage to store images on a local simulator.

For the concept of Blob storage, see Windows azure platform (7) Windows azure storage service.

Before getting started, please make sure that you have downloaded the latest Windows azure development tool, which is 1.6. This section uses Visual Studio 2010 as a development tool.

1. Create an azure Project

Run Visual Studio 2010 as an administrator and create a new windows azure project named azureblobstorage

Select webrole --> right-click --> attribute

On the settings page, add setting, select connection string as the type, and press the rightmost "..." button.

Select "Use the Windows azure storage emulator" and then select "OK"

Then add another setting (setting) called containername and change its type to "string". Set the value to gallery.

Note:

Container and blob naming rules!

 

2. Open default. aspx. CS

Add referenced namespace

 
UsingMicrosoft. windowsazure. storageclient;
UsingMicrosoft. windowsazure;
UsingMicrosoft. windowsazure. serviceruntime;
UsingSystem. Collections. Specialized;
UsingMicrosoft. windowsazure. storageclient. Protocol;

Add the following controls to the front-end aspx:

Then addCodeContent:

 Namespace Mywebrole
{
Public Partial Class _ Default: system. Web. UI. Page
{
Protected Void Page_load ( Object Sender, eventargs E)
{
If (! Ispostback)
{
This . Ensurecontainerexists ();
}
}

Private Void Ensurecontainerexists ()
{
VaR Container = getcontainer ();

// Check whether the container is created. If not, create a container.
Container. createifnotexist ();

VaR Permissions = container. getpermissions ();
// The permission to access storage is to browse the container
Permissions. publicaccess = blobcontainerpublicaccesstype. container;

Container. setpermissions (permissions );
}

Private Cloudblobcontainer getcontainer ()
{
// Obtain serviceconfiguration. cscfg configuration file information
VaR Account = cloudstorageaccount. fromconfigurationsetting ( " Dataconnectionstring " );
VaR Client = Account. createcloudblobclient ();
// Get the blobcontainer object
Return Client. getcontainerreference (roleenvironment. getconfigurationsettingvalue ( " Containername " ));
}

Protected Void Upload_click ( Object Sender, eventargs E)
{
If (Imagefile. hasfile)
{
// Output Image File Information
Status. Text = " Inserted [ " + Imagefile. filename + " ]-Content type [ " + Imagefile. postedfile. contenttype + " ]-Length [ " + Imagefile. postedfile. contentlength + " ] " ;

This . Saveimage (imagefile. filename, imagefile. postedfile. contenttype, imagefile. filebytes );
}
Else
Status. Text = " No Image File " ;
}


Private Void Saveimage ( String Filename, String Contenttype,Byte [] Data)
{
// Obtain the blobcontainer object and upload the file to this container.
VaR Blob = This . Getcontainer (). getblobreference (filename );
Blob. properties. contenttype = contenttype;

// Create metadata
VaR Metadata = New Namevaluecollection ();
Metadata [ " Name " ] = Filename;

// Upload images
Blob. Metadata. Add (metadata );
Blob. uploadbytearray (data );
}
}

3. Open the global. asax. CS file and add the following code:

 VoidApplication_start (ObjectSender, eventargs E)
{
//This code sets up a handler to update cloudstorageaccount instances when their corresponding
//Configuration Settings change in the service configuration file.
Cloudstorageaccount. setconfigurationsettingpublisher (configname, configsetter) =>
{
//Provide the configsetter with the initial value
Configsetter (roleenvironment. getconfigurationsettingvalue (configname ));
});
}

 

4. Run the applicationProgram, Use fileupload to find the local image, and then click "Upload image". The image can be uploaded to the blob of the local storage emulator.

 

6. Open Visual Studio, expand the Server Explorer list on the left, and expand windows azure storage> development> blob> Gallery.

Here, developement indicates that I am using a local simulator.

Gallery is the Blob container I set earlier.

 

Select azure.jpg, right-click the attribute, and you can see the local URL after using storage emluator.

You can open a local browser to view the information.

 

5. Download my project file azureblobstorage.rar
 

 

 

 

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.