博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Windows Azure Storage (2) Windows Azure Storage Service存储服务之Blob详解(上)
阅读量:6909 次
发布时间:2019-06-27

本文共 4401 字,大约阅读时间需要 14 分钟。

  《》

 

  如果读者使用的是国内由世纪互联运维的Azure China服务,请参考笔者的博文

  如果需要参考Azure China使用SAS Token的Sample Code,请参考笔者的博文:

 

 

  本章我们会介绍如何在本地模拟器使用Blob Storage存储图片。

  关于Blob Storage的概念,请参考 。

  在开始介绍之前,请确保您已经下载了最新的Windows Azure开发工具,我使用的是1.6版本。本次介绍使用Visual Studio 2010作为开发工具。

 

Azure Container属性有三种

-  Private:不允许匿名用户读取该容器中的Blob;

-  Public Container:匿名用户可以读取该Container,并且可以列出Container所有Blob内容;

-  Blob:匿名用户只能读取Blob,即只能根据Blob的URL来读取Blob,无法列出Container下所有的Blob。

 

 

1.新建Azure Project

以管理员身份运行Visual Studio 2010,并新建一个Windows Azure Project,我命名为AzureBlobStorage

然后选择WebRole-->右键-->属性

在Settings页面,Add Setting,类型选择Connection String,并且按最右边的"..."按钮

选择"Use the Windows Azure storage emulator",然后选择"OK"

然后添加另外一个Setting(设置),叫做ContainerName并且把它的Type改成"String",值设置成gallery

 

注意:

Container和Blob的命名规则!

 

2.打开Default.aspx.cs

添加引用命名空间

using Microsoft.WindowsAzure.StorageClient; using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.ServiceRuntime; using System.Collections.Specialized; using Microsoft.WindowsAzure.StorageClient.Protocol;

前端的ASPX添加如下控件:

然后添加代码内容:

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();            // 检查container是否被创建,如果没有,创建container            container.CreateIfNotExist();            var permissions = container.GetPermissions();            //对Storage的访问权限是可以浏览Container            permissions.PublicAccess = BlobContainerPublicAccessType.Container;            container.SetPermissions(permissions);        }        private CloudBlobContainer GetContainer()        {            //获取ServiceConfiguration.cscfg配置文件的信息            var account = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");            var client = account.CreateCloudBlobClient();            //获得BlobContainer对象            return client.GetContainerReference(RoleEnvironment.GetConfigurationSettingValue("ContainerName"));        }        protected void upload_Click(object sender, EventArgs e)        {            if (imageFile.HasFile)            {                //输出图片文件的信息                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)        {            //获得BlobContainer对象并把文件上传到这个Container            var blob = this.GetContainer().GetBlobReference(fileName);            blob.Properties.ContentType = contentType;            // 创建元数据信息            var metadata = new NameValueCollection();            metadata["Name"] = fileName;            // 上传图片            blob.Metadata.Add(metadata);            blob.UploadByteArray(data);        }    //发现Azure Storage升级到2.0后API变了      private void SaveImage(string fileName, string contentType, byte[] data)        {            //获得BlobContainer对象并把文件上传到这个Container            var blob = this.GetContainer().GetBlockBlobReference(fileName);            blob.Properties.ContentType = contentType;            using (var ms = new MemoryStream(data, false))            {                blob.UploadFromStream(ms);            }        }    }

 

 3.打开Global.asax.cs文件,添加如下代码

void Application_Start(object sender, 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.运行应用程序,使用FileUpload查找本地图片,然后点击"Upload Image",图片就能上传到本地Storage Emulator的Blob里了。

 

6.打开Visual Studio,展开左侧的Server Explorer列表,依次展开Windows Azure Storage-->Development-->Blob-->gallery

其中Developement表示我使用的是本地的模拟器

gallery就是我前面设置的Blob Container

 

我们选中Azure.jpg,右键属性,可以看使用Storage Emluator后在本地的URL

我们可以通过打开本地的浏览器来查看。

 

5.下载我的工程文件:

 

 

 

 

转载地址:http://kdfcl.baihongyu.com/

你可能感兴趣的文章
Linux 学习之路:认识shell和bash
查看>>
POJ 3041(最小点覆盖)
查看>>
Viewing the interface of your Swift code,查看Swift代码的头文件的三种方法
查看>>
Custom Accessories
查看>>
【转】xcode APP 打包以及提交apple审核详细流程(新版本更新提交审核)
查看>>
DirectX 3D 之C#开发
查看>>
隐藏nginx 版本号信息(转)
查看>>
转:Java中的Clone()方法详解
查看>>
ping命令
查看>>
【转】PHP网站(nginx、php-fpm、mysql) 用户权限解析
查看>>
Spring Boot项目的打包和部署
查看>>
元素绝对居中终极办法兼容IE8
查看>>
weblogic 的应用 常见问题处理 db2 链接不上(转载)
查看>>
linux下的Shell编程(5)循环
查看>>
Switch 语句
查看>>
[PHP] 网盘搜索引擎-采集爬取百度网盘分享文件实现网盘搜索(二)
查看>>
二叉堆
查看>>
Java中的Enum的继承
查看>>
[Android]RecyclerView的简单演示样例
查看>>
怎样在Java中运行Hive命令或HiveQL
查看>>