Azure学习笔记:Storage(3)
2014-03-18 09:46
351 查看
Azure的Storage为我们提供了三种类型的存储结构:Blob类型,Table类型,Queue类型。Blob类型的存储结构非常适合储存文件资料,例如图片或者是文档什么的。Table类型的结构和传统的关系数据库系统中的Table还是有比较大的差别,这里的Table更像C#中的锯齿数组:每行的列都可以自由定制,非常方便。
Blob
在使用Blob的时候,我们需要先创建一个Container,Container有公有和私有之分。它们的功能故名思意都是用来做访问控制的。下面的代码片段演示怎么在Client端调将文件上传至Azure的Blob上。
Common
Upload
Download
Delete
Table
在之前已经介绍过Azure上的Table,在这里它更像是一个对象池,用来保存不同的对象。下面贴一个简单使用它的小例子:使用Azure Table保存员工的信息,并将它绑定到数据呈递控件。
Common
Create Table
Query Table
Update Item
Delete Item
Queue
队列,这里的队列是从数据存储的意义上去定义的,也就是队列类型的数据结构。用户可以使用这种数据结构来处理特殊的业务场景,比如日常任务队列,异常信息队列等等。
Azure Storage Explorer
该工具是款开源的辅助工具,用它可以浏览Azure Storage上保存的数据,是款不错的UI工具。
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 Explorer
该工具是款开源的辅助工具,用它可以浏览Azure Storage上保存的数据,是款不错的UI工具。
相关文章推荐
- OpenGL超级宝典第七版学习笔记-着色器存储块(shader storage block)
- [Android新手学习笔记35]-Storage-SQLite
- [Android新手学习笔记36]-Storage-LitePal
- 第一讲:Azure上创建虚拟机(学习笔记)
- 大数据学习笔记(十)-Hive中的Storage format
- React-Native学习笔记之:AsyncStorage数据存取操作
- Dynamic CRM 2015学习笔记(1)Azure 上安装 CRM 2015
- SMP3.0学习笔记之十 使用Kapsel EncryptedStorage插件以及GET/POST对OData服务进行操作
- Windows 8学习笔记(四)--Storage数据存储
- Azure学习笔记:Web Site(1)
- Azure学习笔记:Cloud Service(4)
- Azure学习笔记:Service Bus(2)
- Inside.MySQL_InnoDB.Storage.Engine 学习笔记
- 通过构建Storage Explorer 应用学习Windows Azure Storage APIs
- 【OpenCV学习笔记】【函数学习】四(origin+imageData+cvcopy()函数+CvMemStorage 类型+CvPoint类型)
- Windows 8学习笔记(四)--Storage数据存储
- [Android新手学习笔记33]-Storage-文件存储
- HTML5+ 学习笔记3 storage.增删改查
- [Android新手学习笔记34]-Storage-SharedPreferences
- linux 学习笔记