教你实现一个简单的云计算程序
来源:互联网 发布:淘宝客链接转化淘口令 编辑:程序博客网 时间:2024/06/15 23:40
准备工作:
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 :
http://www.microsoft.com/downloads/details.aspx?FamilyID=772990da-8926-4db0-958f-95c1da572c84&displaylang=en
一旦你安装成功,你的电脑上会多出Windows Azure SDK v1.1 ,如下图:
启动Development Fabric和Development Stroage:
显示配置界面:
Fabric:
Storage:
以上证明你安装成功。
第二步:安装Windows Azure Tools for Microsoft Visual Studio
地址:http://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 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;
}
}
}
3、在页面上拖放几个控件,简单地布局如下:
4、后台代码如下:
{
// 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);
}
运行效果:
上传:
- 教你实现一个简单的云计算程序
- 一个Nodejs的简单计算测试程序
- 手把手教你做一个简单的VB数据库程序
- JAVA实现简单的计算行数程序
- 计算程序耗时的简单实现代码
- 一个mfc实现的简单计算功能
- python实现一个简单泰勒的计算
- 赛码网,简单计算,用C语言自己实现的一个程序,仅供参考
- 一步一步教你使用CGI实现一个简单的后门
- 一步一步教你使用CGI实现一个简单的后门
- 教你用Django实现一个简单的GIS功能
- 一个简单的程序教你检测你的机器是大端还是小端?
- 从零开始一步步教你:用C++开发一个简单的hadoop分布式计算应用程序
- 从零开始一步步教你:用C++开发一个简单的hadoop分布式计算应用程序
- Linux下计算程序运行时间的一个简单方法
- 一个屏蔽广告程序的简单实现
- 一个简单的时钟程序《VC实现》
- 实现一个简单的屏幕截图程序
- 浅谈C++类 ---- 隐式类类型转换
- Freetype2渲染Unicode初步
- 毕业设计
- FLEX的.NET开源库FluorineFX
- Ubuntu字体的配置
- 教你实现一个简单的云计算程序
- HTML中小meta的大作用
- Android.mk 中拷贝文件到另一个目录
- 一些javascript题目
- Flex与.NET互操作:Flex和.NET协同开发利器FluorineFx
- JavaIO、JavaGUI、Java事件处理学习心得
- PL/SQL最差实践14例
- Sun JDK类库学习(三) 集合类中的Set
- 有点难 我会努力的