准备
OS:Windows7/8, windows server 2008/2012
拥有一个Azure账户
创建一个Azure存储账号
1. 登录Azure管理门户
2. 在左下角点击新建
3.选择data service->storage新建一个存储账户。为其起个名字,这个名字不能跟其他存储账户重名。Region可以任意选一个,建议选East Asia(香港)
4.下面的Enable Geo-Replication如果选中,则Azure会进行异地存储复制。否则只会在本地进行复制。这二者的成本不同。这个选项在存储创建后也可以更改。现在保持缺省值即可。
5.点击Create Storage Account
6.等待账户建立完毕
7.待状态变为Online后,点击进入该账户
8.在存储账户的管理视图中,可以看到Blob/Table/Queue三种服务的URL,还可以看到Geo-Replication的状态以及该账户的主存储位置(East Asia)。中间的监控视图是空的,因为还没有配置监控策略。
9.点击Configure,可以对其进行配置。配置项包括:是否启用异地复制;是否打开监控;是否记录日志。打开异地复制后,可以看到目标数据中心。打开监控后,就可以在主界面和Monitor界面看到监控数据了,包括可用性、性能等。打开日志后,客户端的访问请求就会被记录下来,Azure会在当前存储账户的Blob里建立一个container来存储这些日志。
10.进入containers,可以浏览现有Blob服务的container。目前是空的,可以创建一个
11.创建以后可以看到这个container的路径,还可以点击进去看里面的Blob。目前是空的。
12.目前不支持从管理界面直接上传Blob,我们可以通过一些工具对Blob进行存取。
13.打开Visual studio,打开服务器资源管理器(Server Explorer)。在里面可以看到“Windows Azure 存储”这一项
14.右键Windows Azure,选择“添加新存储账户”
15.在对话框中选择“手动输入凭据”
16.账户名和秘钥可以在Azure管理门户上找到(点击底部的Manage Keys,对话框中第一行是账户名,第二行是主Key,第三行是备用Key。点击Key右侧的按钮可以拷贝)
17.添加账户后就可以看到账户里面的信息了
18.右键Blob,创建一个Container,然后双击打开它
19.在这里可以进行文件的上传、下载和查询。现在上传一个图片试试。点击顶部的箭头按钮,选择一个图片上传。
20.在下面的状态栏里可以看到上传进度。
21.上传完毕后,可以右键另存或者直接打开。
22.也可以复制URL,在浏览器里打开,(如果无法访问,需在Azure界面里面把该container的属性改为Public container)
23.另外,通过Visual studio,还可控制Azure Storage本机的一个模拟器。这个模拟器可以用来开发测试,不需要真正连接Azure
24.这个模拟器需要先启动才能从Visual studio访问
编程访问Blob
1.下面创建一个Visual Studio项目对Blob进行控制。Visual studio中,文件->新建项目->C#->Windows->控制台应用程序,项目名为StorageTest
2.项目建好后,加载storage库到项目。在解决方案资源管理器中展开,右键引用->添加引用
3.查找storage,选中microsoft.windowsazure.storage后安装
3.用Nuget加载OData库,在Visual studio->工具->库程序包管理器->控制台。在PM> 提示符下执行
Install-Package Microsoft.Data.OData -Version 5.0.2
4.打开Program.cs,添加类引用
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
5.在Program类中,增加如下变量
public CloudStorageAccount storageAccount;
6.在Main方法中添加如下内容,其中CloudStorageAccount.DevelopmentStorageAccount是Azure存储服务本地模拟器的账户名
Program test = new Program();
test.storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
test.TestBlob();
Console.ReadKey();
7.增加testBlob方法。该方法首先创建一个container,然后上传一个文件,最后列出所有文件
public void TestBlob()
{
//Create blob client
CloudBlobClient blobclient = storageAccount.CreateCloudBlobClient();
//Create container if not exist
CloudBlobContainer container = blobclient.GetContainerReference("testcontainer");
container.CreateIfNotExists();
String myfile = "test.txt";
// Retrieve reference to a blob named "myblob".
CloudBlockBlob blob = container.GetBlockBlobReference(myfile);
// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(myfile))
{
blob.UploadFromStream(fileStream);
}
// Loop over items within the container and output the length and URI.
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
if (item.GetType() == typeof(CloudBlockBlob))
{
CloudBlockBlob blockBlob = (CloudBlockBlob)item;
Console.WriteLine("Block blob of length {0}: {1}", blockBlob.Properties.Length, blockBlob.Uri);
}
else if (item.GetType() == typeof(CloudPageBlob))
{
CloudPageBlob pageBlob = (CloudPageBlob)item;
Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri);
}
else if (item.GetType() == typeof(CloudBlobDirectory))
{
CloudBlobDirectory directory = (CloudBlobDirectory)item;
Console.WriteLine("Directory: {0}", directory.Uri);
}
}
}
8.在源代码的Debug目录里,建立一个test.txt,作为上传的测试文件
9.在Visual studio里面按F5,运行该程序,应该可以获得类似下面的输出
10.在Visual Studio的服务器资源管理器里面,找到Windows Azure Storage->开发->blob,应该可以看到testcontainer,打开后可以看到blob列表
编程访问Table
1.继续基于上面的项目开发。添加类引用
using Microsoft.WindowsAzure.Storage.Table;
using Microsoft.WindowsAzure.Storage.Table.DataServices;
2.添加类定义,这个类会作为Table要存储的对象结构
public class CustomerEntity : TableEntity
{
public CustomerEntity(string lastName, string firstName)
{
this.PartitionKey = lastName;
this.RowKey = firstName;
}
public CustomerEntity() { }
public string Email { get; set; }
public string PhoneNumber { get; set; }
}
3.添加testTable方法。该方法首先建立一个table,然后加入一个对象,最后查询该表
public void TestTable()
{
// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
String tableName = "Customer";
// Create the CloudTable object that represents the table.
CloudTable table = tableClient.GetTableReference(tableName);
// Create a new customer entity.
CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
customer1.Email = "Walter@contoso.com";
customer1.PhoneNumber = "425-555-0101";
// Create the TableOperation that inserts the customer entity.
TableOperation insertOperation = TableOperation.Insert(customer1);
// Execute the insert operation.
table.Execute(insertOperation);
// Construct the query operation for all customer entities where PartitionKey="Smith".
TableQuery<CustomerEntity> query = new TableQuery<CustomerEntity>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Harp"));
// Print the fields for each customer.
foreach (CustomerEntity entity in table.ExecuteQuery(query))
{
Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey,
entity.Email, entity.PhoneNumber);
}
}
4.在Main方法中加入对该方法的调用
static void Main(string[] args)
{
Program test = new Program();
test.storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
//test.TestBlob();
test.TestTable();
Console.ReadKey();
}
5.按F5运行,应该获得类似下面的结果
6.在Visual Studio的服务器资源管理器里面,找到Windows Azure Storage->开发->table,应该可以看到这个表,打开后可以看到表内容
编程访问Queue
1.添加类引用
using System.Threading;
using Microsoft.WindowsAzure.Storage.Queue;
2.添加TestQueue方法。该方法创建一个队列,然后创建一个线程用于接收消息,同时主线程发送10个消息
public void TestQueue()
{
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
CloudQueue cq = queueClient.GetQueueReference("testqueue");
cq.CreateIfNotExists();
//Receive
Thread td = new Thread(new ThreadStart(() =>
{
Thread.Sleep(3000);
CloudQueueMessage msg1 = cq.GetMessage();
while (msg1 != null)
{
Console.WriteLine(string.Format("{0} Receive:{1}", DateTime.Now, msg1.AsString));
cq.DeleteMessage(msg1);
msg1 = cq.GetMessage();
Thread.Sleep(3000);
}
Console.WriteLine("Receive Completed.");
}));
td.Start();
//Send
for (int i = 0; i < 10; i++)
{
CloudQueueMessage msg = new CloudQueueMessage(i.ToString());
cq.AddMessage(msg);
Console.WriteLine(string.Format("{0} Send:{1}", DateTime.Now, i));
Thread.Sleep(2000);
}
Console.WriteLine("Add Completed.");
}
}
3.在Main方法中加入对该方法的调用
static void Main(string[] args)
{
Program test = new Program();
test.storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
//test.TestBlob();
//test.TestTable();
test.TestQueue();
Console.ReadKey();
}
4.按F5运行,应该获得类似下面的结果
5.在Visual Studio的服务器资源管理器里面,找到Windows Azure Storage->开发->queue,应该可以看到这个队列,双击后可以看到队列内容
将本地存储模拟器切换为Azure存储
以上的测试是在本地模拟器上测试的,要将测试对象切换为真正的Azure存储,只需将Main方法的这句
test.storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
变更为
test.storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=mytest3939393;AccountKey=sZjfJiETVsXCZEmDq8fKNQszlRPCvm4yfjdajf;dajfdjwiPvcU3fvxVHqGmJhIcaVaX3g==");
后面的字符串是Azure存储账户的地址。该地址可以这样获得:在Visual Studio的服务器资源管理器里面,找到Windows Azure Storage,找到最开始添加的Azure存储账户,右键,然后复制红色框里的内容
然后,再运行程序试试