云计算 频道

一步一步教你实现一个简单的云服务

  2、将WebRole的代码修改成如下所示:

public class WebRole : RoleEntryPoint
     {
        
public override bool OnStart()
         {
             DiagnosticMonitor.Start (
"BlobConnectionString");
             CloudStorageAccount.SetConfigurationSettingPublisher((configName,  configSetter) 
=>
             {
                 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;
             }
         }
     }
本文来自编程入门网:http:
//www.bianceng.cn/Servers/cloud-computing/201011/20499_5.htm

 

  3、在页面上拖放几个控件,简单地布局如下:

一步一步教你实现一个简单的云服务(4)

  4、后台代码如下:

protected void Button1_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 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 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);
         }

本文来自编程入门网:http:
//www.bianceng.cn/Servers/cloud-computing/201011/20499_5.htm
0
相关文章