当前位置:   article > 正文

Azure学习笔记:Storage(3)_cloudstorageaccount

cloudstorageaccount

    Azure的Storage为我们提供了三种类型的存储结构:Blob类型,Table类型,Queue类型。Blob类型的存储结构非常适合储存文件资料,例如图片或者是文档什么的。Table类型的结构和传统的关系数据库系统中的Table还是有比较大的差别,这里的Table更像C#中的锯齿数组:每行的列都可以自由定制,非常方便。

Blob

    在使用Blob的时候,我们需要先创建一个Container,Container有公有和私有之分。它们的功能故名思意都是用来做访问控制的。下面的代码片段演示怎么在Client端调将文件上传至Azure的Blob上。

Common

  1. private string storageConnStr = @"DefaultEndpointsProtocol=https;AccountName=ghstorage01;AccountKey=MM2FlgiOMSEpgtcJr65l6/0evAgT8ulIJCVfXAJva7jsFj1Tj6TbN2Imh1/xkhcToeMS64X6DbSRthRGs9IBuw==";
  2. private CloudStorageAccount storageAccount;

Upload

  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3. storageAccount = CloudStorageAccount.Parse(storageConnStr);
  4. //storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
  5. CloudBlobClient client = storageAccount.CreateCloudBlobClient();
  6. CloudBlobContainer container = client.GetContainerReference("pcontainer01");
  7. container.CreateIfNotExists();
  8. CloudBlockBlob blob = container.GetBlockBlobReference(textBox1.Text);
  9. FileStream fs = File.OpenRead(textBox1.Text);
  10. blob.UploadFromStream(fs);
  11. fs.Close();
  12. MessageBox.Show("done");
  13. }

Download

  1. private void button2_Click(object sender, EventArgs e)
  2. {
  3. storageAccount = CloudStorageAccount.Parse(storageConnStr);
  4. CloudBlobClient client= storageAccount.CreateCloudBlobClient();
  5. CloudBlobContainer container = client.GetContainerReference("pcontainer01");
  6. Uri fileUri = new Uri(@"http://ghstorage01.blob.core.windows.net/pcontainer01/C:/Users/v-jexion/Pictures/email/20140103-153640-0001.png");
  7. CloudBlockBlob blockBlob = new CloudBlockBlob(fileUri, new StorageCredentials(storageConnStr));
  8. blockBlob.DownloadToFile(@"d:\tmp.jpg", FileMode.Create);
  9. }

Delete

  1. private void button3_Click(object sender, EventArgs e)
  2. {
  3. storageAccount = CloudStorageAccount.Parse(storageConnStr);
  4. CloudBlobClient client = storageAccount.CreateCloudBlobClient();
  5. CloudBlobContainer container = client.GetContainerReference("pcontainer01");
  6. var blob=container.GetBlockBlobReference("C:/Users/v-jexion/Pictures/email/20140103-153640-0001.png");
  7. blob.Delete();
  8. }


Table

    在之前已经介绍过Azure上的Table,在这里它更像是一个对象池,用来保存不同的对象。下面贴一个简单使用它的小例子:使用Azure Table保存员工的信息,并将它绑定到数据呈递控件。

Common

  1. private string accountConnectionString = @"DefaultEndpointsProtocol=https;AccountName=ghstorage01;AccountKey=MM7FlgiOMSEpgtcJr65l6/0evAgT8ulIJCVfXAJva7jsFj1Tj6TbN2Imh1/xkhcToeMS64X6DbSRthRGs9IBuw==";
  2. CloudStorageAccount account;
  3. public class EmployeeEntity : TableEntity
  4. {
  5. public EmployeeEntity() { }
  6. public EmployeeEntity(string city, string id)
  7. {
  8. this.PartitionKey = city;
  9. this.RowKey = id;
  10. }
  11. public string Name { get; set; }
  12. public int Age { get; set; }
  13. public string Country { get; set; }
  14. }

Create Table

  1. private void btnCreateTable_Click(object sender, EventArgs e)
  2. {
  3. account = CloudStorageAccount.Parse(accountConnectionString);
  4. CloudTableClient client=account.CreateCloudTableClient();
  5. CloudTable table=client.GetTableReference("Employees");
  6. table.CreateIfNotExists();
  7. EmployeeEntity e1=new EmployeeEntity("Beijin","101"){ Name="jeff", Age=27};
  8. TableOperation insertOpt=TableOperation.Insert(e1);
  9. table.Execute(insertOpt);
  10. MessageBox.Show("done");
  11. }


Query Table

  1. private void btnQuery_Click(object sender, EventArgs e)
  2. {
  3. account = CloudStorageAccount.Parse(accountConnectionString);
  4. CloudTableClient client = account.CreateCloudTableClient();
  5. CloudTable table = client.GetTableReference("Employees");
  6. table.CreateIfNotExists();
  7. TableQuery<EmployeeEntity> t = new TableQuery<EmployeeEntity>();
  8. var result = t.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Beijin"));
  9. var list = table.ExecuteQuery(result).ToList();
  10. dataGridView1.DataSource = list;
  11. }

Update Item

  1. private void btnUptdate_Click(object sender, EventArgs e)
  2. {
  3. account = CloudStorageAccount.Parse(accountConnectionString);
  4. CloudTableClient client = account.CreateCloudTableClient();
  5. CloudTable table = client.GetTableReference("Employees");
  6. table.CreateIfNotExists();
  7. TableQuery<EmployeeEntity> t = new TableQuery<EmployeeEntity>();
  8. var result = t.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Beijin"));
  9. var item = table.ExecuteQuery(result).FirstOrDefault();
  10. if (item != null)
  11. {
  12. item.Name += DateTime.Now.ToShortDateString();
  13. TableOperation o = TableOperation.Merge(item);
  14. table.Execute(o);
  15. }
  16. }

Delete Item

  1. private void btnDelete_Click(object sender, EventArgs e)
  2. {
  3. account = CloudStorageAccount.Parse(accountConnectionString);
  4. CloudTableClient client = account.CreateCloudTableClient();
  5. CloudTable table = client.GetTableReference("Employees");
  6. table.CreateIfNotExists();
  7. TableQuery<EmployeeEntity> t = new TableQuery<EmployeeEntity>();
  8. var result = t.Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Beijin"));
  9. var item = table.ExecuteQuery(result).FirstOrDefault();
  10. if (item != null)
  11. {
  12. TableOperation o = TableOperation.Delete(item);
  13. table.Execute(o);
  14. }
  15. }



Queue

     队列,这里的队列是从数据存储的意义上去定义的,也就是队列类型的数据结构。用户可以使用这种数据结构来处理特殊的业务场景,比如日常任务队列,异常信息队列等等。

  1. cloudAcc = CloudStorageAccount.Parse(storageConnStr);
  2. CloudQueueClient queueClient = cloudAcc.CreateCloudQueueClient();
  3. //create queue
  4. CloudQueue queue = queueClient.GetQueueReference("ghstorage01"); // new CloudQueue(new Uri("http://ghstorage01.queue.core.windows.net/"));
  5. queue.CreateIfNotExists();
  6. //enqueue
  7. if (queue != null)
  8. {
  9. CloudQueueMessage msg=new CloudQueueMessage("hello storage queue");
  10. queue.AddMessage(msg);
  11. }
  12. //dequeue
  13. CloudQueueMessage msg2 = queue.GetMessage();
  14. if (msg2 != null)
  15. {
  16. MessageBox.Show(msg2.AsString);
  17. }


Azure Storage Explorer

      该工具是款开源的辅助工具,用它可以浏览Azure Storage上保存的数据,是款不错的UI工具。




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/284385
推荐阅读
相关标签
  

闽ICP备14008679号