Step by step teach you how to implement a simple cloud service

Source: Internet
Author: User
Tags windows azure sdk azure sdk

Preparations:

Windows azure can only run on Windows 7, Windows Server 2008, and Windows Vista. Windows 2003 and XP are currently not supported. I installed Windows 7 yesterday and tried to write a small azureProgramThe program is very simple and implements the simple upload and search functions of images. I will study Microsoft's azure platform by the way. below is my software environment.

Operating System: Windows 7

Development tools:Visual studio2010 RC, Windows azure SDK, Windows azure tools

Step 1: Install the Windows azure SDK

First, make sure that your operating system meets the requirements,Visual Studio can beVisual Studio2008 orVisual Studio2010. Download the Windows azure SDK at the following URL:

Http://www.microsoft.com/downloads/details.aspx? Familyid = 7729904158926-4db0-958f-95c1da572c84 & displaylang = en

Once you have successfully installed Windows azure SDK V1.1 on your computer, for example:

Start DevelopmentFabric and Development Stroage:

The configuration page is displayed:

Development fabric:

DevelopmentStorage:

The above proves that the installation is successful.

Step 2: Install Windows azure tools for Microsoft Visual Studio

Http://www.microsoft.com/downloads/details.aspx? Familyid = 5664019e-6860-4c33-9843-4eb40b297ab6 & displaylang = en

After installation,Visual Studio hasThe template is as follows:


Step 3: Create a cloud serviceName it blobstorage, for example:

Use simple ASP. NET web role and rename it blobwebrole, for example:

Project with the following structure:

Step 4: ModifyCode, We implement a simple image upload and search function:

1. Add a connectionstring, as shown in figure

2. Modify the webrole Code as follows:

Code

  Public   Class Webrole: roleentrypoint
{
Public   Override   Bool Onstart ()
{
Diagnosticmonitor. Start ( " Blobconnectionstring " );

cloudstorageaccount. setconfigurationsettingpublisher (configname, configsetter) =>
{< br> configsetter (roleenvironment. getconfigurationsettingvalue (configname);
});

// get the Blob connection string
cloudstorageaccount objstorage = cloudstorageaccount. fromconfigurationsetting ( " blobconnectionstring " );

// get the Client Reference
cloudblobclient objclient = New cloudblobclient (objstorage. blobendpoint, objstorage. credentials);

// get the reference to container
cloudblobcontainer objcontainer = objclient. getcontainerreference ( " mycontainer " );

//Create the container if it does not exist
Objcontainer. createifnotexist ();

Roleenvironment. Changing+ =Roleenvironmentchanging;

Return Base. Onstart ();

}

Private   Void Roleenvironmentchanging ( Object Sender, roleenvironmentchangingeventargs E)
{
// If a configuration setting is changing
If (E. Changes. Any (Change => Change Is Roleenvironmentconfigurationsettingchange ))
{
// Set E. Cancel to true to restart this role instance
E. Cancel =   True ;
}
}
}

3. Drag and Drop several controls on the page to make a simple layout as follows:

4. The background code is as follows:

Code

Protected   Void Button#click ( Object Sender, eventargs E)
{

// Get the storage account reference
Cloudstorageaccount objstorage = Cloudstorageaccount. fromconfigurationsetting ( " Blobconnectionstring " );
// Get the Client Reference using storage blobend point
Cloudblobclient objclient =   New Cloudblobclient (objstorage. blobendpoint, objstorage. Credentials );
// Get container reference
Cloudblobcontainer objcontainer = Objclient. getcontainerreference ( " Mycontainer " );

// Get blob reference
Cloudblob OBJ = Objcontainer. getblobreference (fileupload1.filename. tostring ());

// Set meta values
OBJ. Metadata [ " Metaname " ] =   " Meta " ;
// Open a stream using the cloud object
Blobstream = OBJ. openwrite ();
// Write the stream to the Blob Database
Blobstream. Write (fileupload1.filebytes, 0 , Fileupload1.filebytes. Count ());
Blobstream. Close ();

// Browse through blob list from the container
Ienumerable < Ilistblobitem > Objbloblist = Objcontainer. listblobs ();
Foreach (Ilistblobitem objitem In Objbloblist)
{
Response. Write (objitem. Uri +   " <Br> " );
}

}

Protected   Void Button2_click ( Object Sender, eventargs E)
{
// Get the storage account reference
Cloudstorageaccount objstorage = Cloudstorageaccount. fromconfigurationsetting ( " Blobconnectionstring " );
// Get the Client Reference using storage blobend point
Cloudblobclient objclient =   New Cloudblobclient (objstorage. blobendpoint, objstorage. Credentials );
// Get container reference
Cloudblobcontainer objcontainer = Objclient. getcontainerreference ( " Mycontainer " );
// Get the Blob reference using the Blob name provided in the search
Cloudblob OBJ = Objcontainer. getblobreference (txtsearch. Text );
Blobstream = OBJ. openread ();
// Create the image object and display the same on the browser response
System. Drawing. Image objimg =   Null ;
Objimg = System. Drawing. image. fromstream (blobstream, True );
Response. Clear ();
Response. contenttype =   " Image/GIF " ;
Objimg. Save (response. outputstream, system. Drawing. imaging. imageformat. JPEG );
}

Running effect:

Upload:

Search: search for a beauty Image

Code:/files/zhuqil/blobstorage.zip

 

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.