【云计算】一步一步教你实现一个简单的云服务
小标 2018-11-09 来源 : 阅读 1302 评论 0

摘要:本文主要向大家介绍了【云计算】一步一步教你实现一个简单的云服务,通过具体的内容向大家展现,希望对大家学习云计算有所帮助。

本文主要向大家介绍了【云计算】一步一步教你实现一个简单的云服务,通过具体的内容向大家展现,希望对大家学习云计算有所帮助。


  准备工作:Windows Azure只能运行在Windows 7, Windows Server 2008 和Windows Vista 上。暂不支持Windows 2003和XP。昨天安装了Win7,打算尝试写一个 Azure小程序,程序很简单,实现图片的简单上传和搜索功能,我顺便研究一下 微软的Azure平台,下面是我的软件环境。


  操作系统:Windows 7


  开发工具:Visual Studio2010 RC,Windows Azure SDK ,Windows Azure Tools


  第一步:安装Windows Azure SDK


  首先确定你的操作系统满足要求,Visual Studio可以是Visual Studio2008 或者Visual Studio2010。到下面网址下载Windows Azure SDK :


  //www.microsoft.com/downloads/details.aspx?FamilyID=772990da- 8926-4db0-958f-95c1da572c84&displaylang=en


  一旦你安装成功,你的电脑上会多出Windows Azure SDK v1.1 ,如下图:





  启动Development Fabric和Development Stroage:





  显示配置界面:





  Development Fabric:







  Development Storage:





  以上证明你安装成功。


  第二步:安装Windows Azure Tools for Microsoft Visual Studio


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


  安装之后,Visual Studio会有如下模版:





  第三步:新建一个云服务命名为BlobStorage,如下图:







  使用简单的ASP.NET Web Role,并更名为BlobWebRole,如下图:





  得到如下结构的项目:







  第四步:修改代码,我们实现一个简单的图片上传和搜索的功能:





  1、添加一个connectionstring,如下图





 




  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;

}

}

}

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



 


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





  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);

}

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标大数据云计算大数据安全频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved