Windows Azure Storage
之前都是在博客园看别人的文章,今天开始就开启自己的博客咯,欢迎阅读,共同探讨!
简单点说Widows Azure Storage就是一个大的网盘,可以让用户存储任何想存储的数据,数据一旦存储到“云”中就永远不会丢失,程序员可以在任何时候,任何终端,任何地点获取任意大小的数据。
目前来说,Windoows Azure Storage 是由4部分组成,如下:
1.Windows Azure Blob Service:主要存储一些大型的Unstructure的数据,Text or Binary,像Media, Document等等。
2.Windows Azure Table Service:以前在开发一些Asp.net程序的时候,大多数会使用关系型数据库(SQL Server),往往一个应用程序中的类对应数据库中的某些表,然后你需要大量的代码或者工具来完成关系数据库到对象之间的映射,这个时候你是多么想直接将实体类存储到数据库当中呀!Windows Azure Table服务就是来解决这个问题的。它可以直接将实体类、实体对象存入到表结构当中,它和一般的关系型数据库中的Table很像,当时也有很大的不同,比如它不能提供像SQL中的Join方法,也不能管理Foreign Key.
3.Windows Azure Queue Service:不同于前两种都是在存储或者读取数据,Queue Service主要是用来存储消息队列,应用得最多的就是为Work Role和Web Role来传递消息。
4.Windows Azure File Storage:提供在云上的文件共享服务,你可以存储任何你需要共享的文件,比如用得比较多的就是共享应用程序的配置文件,日志文件,Crash Dumps等等。同样也可以实现在多台虚拟机上挂载共享存储。
下面分别介绍下这4种存储的结构,如下:
1.Blob Storage Concepts:
Storage Account:所有对Storage的操作都是通过Storage Account来完成的,可以说是Azure Storage的入口.
Container:Container提供了一组Blob的分组,一个Accout能包含无个数限制的Container,一个Container能包含无个数限制的Blo.
Blob:任何类型或者大小的文件。在这里用得比较多的是Block Blob.它是存储text或者Binary的理想选择。Page Blob其实和Black Blob很像,因为它就是Black Blob组成的。比较适用于存储逐渐增长的这种数据,Like Logging.还有一种就是Page Block,对于那种需要频繁的读写操作的存储更加高效,一般作为虚拟机的Data Disk比较多。
2.Table Storage Concepts:
Storage Account:所有对Storage的操作都是通过Storage Account来完成的,可以说是Azure Storage的入口.
Table:存储实体类或者实体对象(Entity)的集合.
Entity:一组属性的实体,类似于数据库的行.
Properties:一个属性就是一个Name-Value的键值对,每个实体(Entity)有3个系统的属性, partition key, a row key, and a timestamp.
3.Queue Storage Concepts
Storage Account:所有对Storage的操作都是通过Storage Account来完成的,可以说是Azure Storage的入口.
Queue:存储message.
4.File Storage Concepts
Storage Account:所有对Storage的操作都是通过Storage Account来完成的,可以说是Azure Storage的入口.
Share:是Azure上的一个File Share的地方,所有的Directory或者File都必须在share里面创建.
Directory:可选的在这种层级里面.
File:需要共享的文件.
最后介绍下这4种结构的创建和一些常见的用法,如下:
这4种存储都可以通过Azure Portal或者Power Shell来创建和操作,具体可以参考:https://azure.microsoft.com/en-us/documentation/articles/storage-introduction/
下面我们介绍以编程的方式分别访问这几种Storage:
1.Blob:
public class BlobStorage
{
public CloudStorageAccount storageAccount;
public CloudBlobClient blobClient;
public CloudBlobContainer container; public void initial()
{
//CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=mystoraget;AccountKey=mykey"); blobClient = storageAccount.CreateCloudBlobClient();
container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExists();
} //Upload a file into a container
public void UploadBlodToContainer()
{
container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
using (var filestream = System.IO.File.OpenRead(@"C:\data.txt"))
{
blockBlob.UploadFromStream(filestream);
}
CloudBlockBlob blockBlob2 = container.GetBlockBlobReference("myblob2");
using (var filestream = System.IO.File.OpenRead(@"C:\MyFolder\mytext.txt"))
{
blockBlob2.UploadFromStream(filestream);
} Console.WriteLine("Upload blob to container succeed!");
} //List the blobs in a container
public void ListBlodFromContainer()
{
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blob = (CloudBlockBlob)item;
Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri);
}
else if (item.GetType() == typeof(CloudPageBlob))
{
CloudPageBlob pageBlob = (CloudPageBlob)item; Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri);
} }
Console.WriteLine("List Blod succeed!");
} //Download file from blobs
public void DownloadToStream()
{
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob"); using (var fileStream = System.IO.File.OpenWrite(@"C:\MyFolder\Inputext.txt"))
{
blockBlob.DownloadToStream(fileStream);
} Console.WriteLine("Download blob to stream succeed!");
} //Delete blob
public void DeleteBlob()
{
CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob"); blockBlob.Delete();
Console.WriteLine("Delete blob succeed!");
}
}
2.Table
public class TableStorage
{
public CloudStorageAccount storageAccount;
public CloudTableClient tableClient;
public CloudTable table; //Create a table
public void initial()
{
storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=mystoraget;AccountKey=mykey"); tableClient = storageAccount.CreateCloudTableClient();
table = tableClient.GetTableReference("people");
table.CreateIfNotExists();
} //Add an entity to a table
public void AddEntityToTable()
{
CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
customer1.Email = "Walter@contoso.com";
customer1.PhoneNumber = "425-555-0101";
TableOperation inserOperation = TableOperation.Insert(customer1); table.Execute(inserOperation);
Console.WriteLine("Insert entity to a table succeed");
} //Insert a batch of entities
public void AddbatchEntityToTable()
{
TableBatchOperation batchOperation = new TableBatchOperation(); CustomerEntity customer1 = new CustomerEntity("Smith", "Jeff");
customer1.Email = "Jeff@contoso.com";
customer1.PhoneNumber = "425-555-0104"; CustomerEntity customer2 = new CustomerEntity("Smith", "Ben");
customer2.Email = "Ben@contoso.com";
customer1.PhoneNumber = "425-555-0102"; batchOperation.Insert(customer1);
batchOperation.Insert(customer2); table.ExecuteBatch(batchOperation);
Console.WriteLine("Insert a batch of entities to a table succeed");
} //Query a table for all Entity
public void QueryEntityFromTable()
{
TableQuery<CustomerEntity> query = new TableQuery<CustomerEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Smith"));
foreach (CustomerEntity entity in table.ExecuteQuery(query))
{
Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey,
entity.Email, entity.PhoneNumber);
}
} //Delete Entity from table
public void DeleteEntityFromTable()
{
TableOperation retrieveOperation = TableOperation.Retrieve<CustomerEntity>("Smith", "Ben");
TableResult retrievedResult = table.Execute(retrieveOperation); CustomerEntity deleteEntity = (CustomerEntity)retrievedResult.Result;
if (deleteEntity != null)
{
TableOperation deleteOperation = TableOperation.Delete(deleteEntity); // Execute the operation.
table.Execute(deleteOperation); Console.WriteLine("Entity deleted.");
} else
Console.WriteLine("Could not retrieve the entity.");
} //delete a table
public void deletetable()
{
table.DeleteIfExists();
Console.WriteLine("Delete table successful!");
}
}
3.Queue
public class QueueStorage
{
public CloudStorageAccount storageAccount;
public CloudQueueClient queueClient;
public CloudQueue queue; public void initial()
{
storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=mystoraget;AccountKey=mykey"); queueClient = storageAccount.CreateCloudQueueClient();
queue = queueClient.GetQueueReference("myqueue");
queue.CreateIfNotExists();
} //Inser a message into a queue
public void InsertMessageToQueue()
{
CloudQueueMessage message = new CloudQueueMessage("Hello,World");
CloudQueueMessage message2 = new CloudQueueMessage("Hello2,World2");
queue.AddMessage(message2);
queue.AddMessage(message);
Console.WriteLine("Insert Message to queue succeed!");
} //get message
public void GetMessage()
{
CloudQueueMessage retrieveMessage = queue.GetMessage();
Console.WriteLine("Retrieved message with content'{0}'", retrieveMessage.AsString);
} //update the content of a queued message
public void UpdateMessage()
{
CloudQueueMessage message = queue.GetMessage();
Console.WriteLine("Retrieved message with content'{0}'", message.AsString);
message.SetMessageContent("Updated.");
queue.UpdateMessage(message, TimeSpan.FromSeconds(10.0), MessageUpdateFields.Content | MessageUpdateFields.Visibility);
Console.WriteLine("Update Message complete, I will appear after 10s");
} //test async
public async Task AsyncPattern()
{
if (await queue.CreateIfNotExistsAsync())
{
Console.WriteLine("Queue '{0}' Created", queue.Name);
}
else
{
Console.WriteLine("Queue '{0}' Exists", queue.Name);
} CloudQueueMessage cloudQueueMessage = new CloudQueueMessage("My message"); await queue.AddMessageAsync(cloudQueueMessage);
Console.WriteLine("Message added"); CloudQueueMessage retrievedMessage = await queue.GetMessageAsync();
Console.WriteLine("Retrieved message with content'{0}'", retrievedMessage.AsString); await queue.DeleteMessageAsync(retrievedMessage);
Console.WriteLine("Deleted message"); }
}
4.File
public class FileStorage
{
public CloudStorageAccount storageAccount;
public CloudFileClient fileClient;
public CloudFileShare share;
public CloudFileDirectory rootDir;
public CloudFileDirectory sampleDir; //Create a table
public void initial()
{
storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=mystoraget;AccountKey=mykey"); fileClient = storageAccount.CreateCloudFileClient();
share = fileClient.GetShareReference("testfiledemo");
share.CreateIfNotExists(); rootDir = share.GetRootDirectoryReference();
sampleDir = rootDir.GetDirectoryReference("Images");
sampleDir.CreateIfNotExists();
} //upload file to directory in file share
public void UploadFile()
{
CloudFile sourceFile = sampleDir.GetFileReference("sample-file.txt");
sourceFile.Create();
using (var filestream = System.IO.File.OpenRead(@"C:\aa.txt"))
{
sourceFile.UploadFromStream(filestream);
}
Console.WriteLine("Upload file to directory succeed!");
}
//Download the file
public void DownloadFile()
{
CloudFile file = sampleDir.GetFileReference("sample-file.txt");
Console.WriteLine(file.DownloadTextAsync().Result);
} //Copy file to a blob
public void CopyFileToBlob()
{
CloudFileShare share = fileClient.GetShareReference("sample-share");
share.CreateIfNotExists(); CloudFile sourceFile = share.GetRootDirectoryReference().GetFileReference("sample-file.txt"); sourceFile.UploadText("A sample file in the root directory."); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("sample-container");
container.CreateIfNotExists();
CloudBlockBlob desBlob = container.GetBlockBlobReference("sample-container"); string fileSas = sourceFile.GetSharedAccessSignature(new SharedAccessFilePolicy()
{
Permissions =SharedAccessFilePermissions.Read,
SharedAccessExpiryTime=DateTime.UtcNow.AddHours()
}); Uri fileSasUri = new Uri(sourceFile.StorageUri.PrimaryUri.ToString()+fileSas); desBlob.StartCopy(fileSasUri); Console.WriteLine("source file contents:{0}", sourceFile.DownloadText());
Console.WriteLine("destination blob contents:{0}",desBlob.DownloadText());
} //delete file share
public void DeleteFileShare()
{
share.Delete(); Console.WriteLine("Delete file share success!");
}
}
}
Windows Azure Storage的更多相关文章
- [New Portal]Windows Azure Storage (14) 使用Azure Blob的PutBlock方法,实现文件的分块、离线上传
<Windows Azure Platform 系列文章目录> 相关内容 Windows Azure Platform (二十二) Windows Azure Storage Servic ...
- [SDK2.2]Windows Azure Storage (15) 使用WCF服务,将本地图片上传至Azure Storage (上) 服务器端代码
<Windows Azure Platform 系列文章目录> 这几天工作上的内容,把项目文件和源代码拿出来给大家分享下. 源代码下载:Part1 Part2 Part3 我们在写WEB服 ...
- Windows Azure Storage (18) 使用HTML5 Portal的Azure CDN服务
<Windows Azure Platform 系列文章目录> Update:2015-04-15 如果读者使用的是国内由世纪互联运维的Azure China服务,请参考笔者的文档:Azu ...
- Windows Azure Storage (19) 再谈Azure Block Blob和Page Blob
<Windows Azure Platform 系列文章目录> 请读者在参考本文之前,预习相关背景知识:Windows Azure Storage (1) Windows Azure St ...
- Windows Azure Storage (21) 使用AzCopy工具,加快Azure Storage传输速度
<Windows Azure Platform 系列文章目录> Update 2016-09-28 想要在Azure云端,使用AzCopy工具,从Azure China 上海数据中心存储账 ...
- Windows Azure Storage (22) Azure Storage如何支持多级目录
<Windows Azure Platform 系列文章目录> 熟悉Azure平台的读者都知道,Azure Blob有三层架构.如下图:(注意blob.core.chinacloudapi ...
- Windows Azure Storage 之 Retry Policy (用来处理短暂性错误-Transient Fault)
在使用Windows Azure Storage Service 的时候, 通常会遇到各种各样的问题. 例如网络连接不稳定,导致请求没有发出去.删除一个Blob Container 之后又立刻创建同名 ...
- Windows Azure Storage图形界面管理工具
上一篇我们介绍了用PowerShell将Windows Azure的存储服务当网盘来使用.如果感觉还不够简单,那么这次我们来看看还有哪些使用起来更方便的图形界面管理工具吧.当然,这些工具必要支持中国版 ...
- [转]探索 Windows Azure Storage
本文转自:https://msdn.microsoft.com/zh-tw/jj573842 概觀 儲存服務 (Storage services) 在 Windows Azure 運算模擬器中提供了可 ...
- Windows Azure Storage (6) Windows Azure Storage之Table
<Windows Azure Platform 系列文章目录> 最近想了想,还是有必要把Windows Azure Table Storage 给说清楚. 1.概念 Windows Azu ...
随机推荐
- 读取同一文件夹下多个txt文件中的特定内容并做统计
读取同一文件夹下多个txt文件中的特定内容并做统计 有网友在问,C#读取同一文件夹下多个txt文件中的特定内容,并把各个文本的数据做统计. 昨晚Insus.NET抽上些少时间,来实现此问题,加强自身的 ...
- Entity Framework 之 Code First
使用NuGet助您玩转代码生成数据————Entity Framework 之 Code First [前言] 如果是Code First老鸟或者对Entity Framework不感兴趣,就不用浪费 ...
- win7 安装 sql2000
win7 安装:http://wenku.baidu.com/link?url=xNcfrMaMzX0KgBcjpMaySRaKITM2Op73ZI8sOX49zgl-GWPGB3vqye9gZA_c ...
- 常用PHP正则表达式
获取所有图片网址preg_match_all(“/ src=(\”|\’){0,}(http:\/\/(.+?))(\”|\’|\s|>)/is”,$text,$img); 匹配中文字符的正则表 ...
- bzoj 1430: 小猴打架 -- prufer编码
1430: 小猴打架 Time Limit: 5 Sec Memory Limit: 162 MB Description 一开始森林里面有N只互不相识的小猴子,它们经常打架,但打架的双方都必须不是 ...
- Oracle基础学习(一)常用函数
1.绝对值:abs() select abs(-2) value from dual; 2.取整函数(大):ceil() select ceil(-2.001) value from dual;(-2 ...
- Linux笔记(二) - 权限管理
(1)改变文件或目录权限:chmod{ugoa}{+-=}{rwx}{mode=421}-R 递归修改可以同时更改多个 chmod g+w a.txt b.txt c.txt例:chmod g=rwx ...
- The Linux Mint 18.1:Eclipse Run The C++ And Python ConfigorationWhen You achieve above step,you can run the c++ and python! (Next OTL ,PYOTL is Project That Write By Ruimin Shen(ability man) )
# Copyright (c) 2016, 付刘伟 (Liuwei Fu)# All rights reserved.# 转载请注明出处 1.Install The Eclipse,g++ Use T ...
- (转)GBDT迭代决策树理解
在网上看到一篇对从代码层面理解gbdt比较好的文章,转载记录一下: GBDT(Gradient Boosting Decision Tree) 又叫 MART(Multiple Additive Re ...
- PAT甲级训练刷题代码记录
刷题链接:https://www.patest.cn/contests/pat-a-practise 1001 #include <iostream> #include <stdio ...