赞
踩
Azure的Storage为我们提供了三种类型的存储结构:Blob类型,Table类型,Queue类型。Blob类型的存储结构非常适合储存文件资料,例如图片或者是文档什么的。Table类型的结构和传统的关系数据库系统中的Table还是有比较大的差别,这里的Table更像C#中的锯齿数组:每行的列都可以自由定制,非常方便。
Blob
在使用Blob的时候,我们需要先创建一个Container,Container有公有和私有之分。它们的功能故名思意都是用来做访问控制的。下面的代码片段演示怎么在Client端调将文件上传至Azure的Blob上。
Common
- private string storageConnStr = @"DefaultEndpointsProtocol=https;AccountName=ghstorage01;AccountKey=MM2FlgiOMSEpgtcJr65l6/0evAgT8ulIJCVfXAJva7jsFj1Tj6TbN2Imh1/xkhcToeMS64X6DbSRthRGs9IBuw==";
- private CloudStorageAccount storageAccount;
Upload
- private void button1_Click(object sender, EventArgs e)
- {
- storageAccount = CloudStorageAccount.Parse(storageConnStr);
- //storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
-
- CloudBlobClient client = storageAccount.CreateCloudBlobClient();
- CloudBlobContainer container = client.GetContainerReference("pcontainer01");
- container.CreateIfNotExists();
-
- CloudBlockBlob blob = container.GetBlockBlobReference(textBox1.Text);
- FileStream fs = File.OpenRead(textBox1.Text);
- blob.UploadFromStream(fs);
- fs.Close();
- MessageBox.Show("done");
-
- }
Download
- private void button2_Click(object sender, EventArgs e)
- {
- storageAccount = CloudStorageAccount.Parse(storageConnStr);
- CloudBlobClient client= storageAccount.CreateCloudBlobClient();
- CloudBlobContainer container = client.GetContainerReference("pcontainer01");
- Uri fileUri = new Uri(@"http://ghstorage01.blob.core.windows.net/pcontainer01/C:/Users/v-jexion/Pictures/email/20140103-153640-0001.png");
- CloudBlockBlob blockBlob = new CloudBlockBlob(fileUri, new StorageCredentials(storageConnStr));
-
- blockBlob.DownloadToFile(@"d:\tmp.jpg", FileMode.Create);
-
-
-
- }
Delete
- private void button3_Click(object sender, EventArgs e)
- {
- storageAccount = CloudStorageAccount.Parse(storageConnStr);
- CloudBlobClient client = storageAccount.CreateCloudBlobClient();
- CloudBlobContainer container = client.GetContainerReference("pcontainer01");
- var blob=container.GetBlockBlobReference("C:/Users/v-jexion/Pictures/email/20140103-153640-0001.png");
- blob.Delete();
-
-
- }
Table
在之前已经介绍过Azure上的Table,在这里它更像是一个对象池,用来保存不同的对象。下面贴一个简单使用它的小例子:使用Azure Table保存员工的信息,并将它绑定到数据呈递控件。
Common
- private string accountConnectionString = @"DefaultEndpointsProtocol=https;AccountName=ghstorage01;AccountKey=MM7FlgiOMSEpgtcJr65l6/0evAgT8ulIJCVfXAJva7jsFj1Tj6TbN2Imh1/xkhcToeMS64X6DbSRthRGs9IBuw==";
- CloudStorageAccount account;
- public class EmployeeEntity : TableEntity
- {
- public EmployeeEntity() { }
-
- public EmployeeEntity(string city, string id)
- {
- this.PartitionKey = city;
- this.RowKey = id;
- }
-
- public string Name { get; set; }
-
- public int Age { get; set; }
-
- public string Country { get; set; }
- }
Create Table
- private void btnCreateTable_Click(object sender, EventArgs e)
- {
- account = CloudStorageAccount.Parse(accountConnectionString);
- CloudTableClient client=account.CreateCloudTableClient();
- CloudTable table=client.GetTableReference("Employees");
- table.CreateIfNotExists();
-
- EmployeeEntity e1=new EmployeeEntity("Beijin","101"){ Name="jeff", Age=27};
-
- TableOperation insertOpt=TableOperation.Insert(e1);
- table.Execute(insertOpt);
- MessageBox.Show("done");
-
- }
Query Table
- private void btnQuery_Click(object sender, EventArgs e)
- {
- account = CloudStorageAccount.Parse(accountConnectionString);
- CloudTableClient client = account.CreateCloudTableClient();
- CloudTable table = client.GetTableReference("Employees");
- table.CreateIfNotExists();
-
- TableQuery<EmployeeEntity> t = new TableQuery<EmployeeEntity>();
- var result = t.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Beijin"));
- var list = table.ExecuteQuery(result).ToList();
- dataGridView1.DataSource = list;
-
- }
Update Item
- private void btnUptdate_Click(object sender, EventArgs e)
- {
- account = CloudStorageAccount.Parse(accountConnectionString);
- CloudTableClient client = account.CreateCloudTableClient();
- CloudTable table = client.GetTableReference("Employees");
- table.CreateIfNotExists();
-
- TableQuery<EmployeeEntity> t = new TableQuery<EmployeeEntity>();
- var result = t.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Beijin"));
- var item = table.ExecuteQuery(result).FirstOrDefault();
-
- if (item != null)
- {
- item.Name += DateTime.Now.ToShortDateString();
- TableOperation o = TableOperation.Merge(item);
- table.Execute(o);
- }
- }
Delete Item
- private void btnDelete_Click(object sender, EventArgs e)
- {
- account = CloudStorageAccount.Parse(accountConnectionString);
- CloudTableClient client = account.CreateCloudTableClient();
- CloudTable table = client.GetTableReference("Employees");
- table.CreateIfNotExists();
-
- TableQuery<EmployeeEntity> t = new TableQuery<EmployeeEntity>();
- var result = t.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Beijin"));
- var item = table.ExecuteQuery(result).FirstOrDefault();
-
- if (item != null)
- {
- TableOperation o = TableOperation.Delete(item);
- table.Execute(o);
- }
- }
Queue
队列,这里的队列是从数据存储的意义上去定义的,也就是队列类型的数据结构。用户可以使用这种数据结构来处理特殊的业务场景,比如日常任务队列,异常信息队列等等。
- cloudAcc = CloudStorageAccount.Parse(storageConnStr);
- CloudQueueClient queueClient = cloudAcc.CreateCloudQueueClient();
-
- //create queue
- CloudQueue queue = queueClient.GetQueueReference("ghstorage01"); // new CloudQueue(new Uri("http://ghstorage01.queue.core.windows.net/"));
- queue.CreateIfNotExists();
-
- //enqueue
- if (queue != null)
- {
- CloudQueueMessage msg=new CloudQueueMessage("hello storage queue");
-
- queue.AddMessage(msg);
-
-
- }
-
- //dequeue
- CloudQueueMessage msg2 = queue.GetMessage();
- if (msg2 != null)
- {
- MessageBox.Show(msg2.AsString);
- }
该工具是款开源的辅助工具,用它可以浏览Azure Storage上保存的数据,是款不错的UI工具。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。