《Windows Azure Platform 系列文章目录

  注意:本文适用于国内由世纪互联运维的Azure China。

  本文将会介绍如何使用REST API来直接访问Storage Service。

  项目文件在这里下载

  1.首先我们以管理员身份,创建一个Windows Console项目

  2.在Program.cs中,增加如下代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web; namespace AzureStorageRestAPI
{
class Program
{ internal class CanonicalizedString
{ private StringBuilder canonicalizedString = new StringBuilder(); internal CanonicalizedString(string initialElement)
{ this.canonicalizedString.Append(initialElement);
} internal void AppendCanonicalizedElement(string element)
{ this.canonicalizedString.Append("\n");
this.canonicalizedString.Append(element);
} internal string Value
{
get
{
return this.canonicalizedString.ToString(); }
} } const string bloburi = @"https://leidemo.blob.core.chinacloudapi.cn";
const string accountname = "leidemo";
const string key = "EZNbnhPJ7+Fv6X5k9OW36ece5WflDJaUvGjdVpdwxEXsKVzEa18/Rw2f30d6ASELNYE7XlvFs78nfCw+UIs3kQ==";
const string method = "GET"; static void Main(string[] args)
{
string AccountName = accountname;
string AccountSharedKey = key;
string Address = bloburi;
string container = "public"; // 创建请求字符串
string QueryString = "?restype=container&comp=list";
Uri requesturi = new Uri(Address + "/" + container + QueryString);
string MessageSignature = ""; // 创建HttpWebRequest类 HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(requesturi.AbsoluteUri); Request.Method = method; Request.ContentLength = ; Request.Headers.Add("x-ms-date", DateTime.UtcNow.ToString("R")); Request.Headers.Add("x-ms-version", "2009-09-19"); // 开始创建签名 MessageSignature += "GET\n"; // Verb MessageSignature += "\n"; // Content-Encoding MessageSignature += "\n"; // Content-Language MessageSignature += "\n"; // Content-Length MessageSignature += "\n"; // Content-MD5 MessageSignature += "\n"; // Content-Type MessageSignature += "\n"; // Date MessageSignature += "\n"; // If-Modified-Since MessageSignature += "\n"; // If-Match MessageSignature += "\n"; // If-None-Match MessageSignature += "\n"; // If-Unmodified-Since MessageSignature += "\n"; // Range // CanonicalizedHeaders ArrayList list = new ArrayList(); foreach (string str in Request.Headers.Keys)
{ if (str.ToLowerInvariant().StartsWith("x-ms-", StringComparison.Ordinal))
{ list.Add(str.ToLowerInvariant()); } } list.Sort(); foreach (string str2 in list)
{ StringBuilder builder = new StringBuilder(str2); string str3 = ":"; foreach (string str4 in GetHeaderValues(Request.Headers, str2))
{ string str5 = str4.Replace("\r\n", string.Empty); builder.Append(str3); builder.Append(str5); str3 = ","; } MessageSignature += builder.ToString() + "\n"; } MessageSignature += GetCanonicalizedResourceVersion2(requesturi, AccountName); // 开始创建签名 byte[] SignatureBytes = System.Text.Encoding.UTF8.GetBytes(MessageSignature); System.Security.Cryptography.HMACSHA256 SHA256 = new System.Security.Cryptography.HMACSHA256(Convert.FromBase64String(AccountSharedKey)); // 创建Authorization HTTP消息头的值 String AuthorizationHeader = "SharedKey " + AccountName + ":" + Convert.ToBase64String(SHA256.ComputeHash(SignatureBytes)); // 把编码后的签名加入到Authorization HTTP消息头中 Request.Headers.Add("Authorization", AuthorizationHeader); // 获取返回消息 using (HttpWebResponse response = (HttpWebResponse)Request.GetResponse())
{ if (response.StatusCode == HttpStatusCode.OK)
{ // 服务器返回成功消息 using (Stream stream = response.GetResponseStream())
{ using (StreamReader sr = new StreamReader(stream))
{ var s = sr.ReadToEnd(); // 输出返回消息 Console.WriteLine(s); } } } else
{ // 这里可以抛出异常信息 } } Console.ReadLine(); } static ArrayList GetHeaderValues(NameValueCollection headers, string headerName)
{ ArrayList list = new ArrayList(); string[] values = headers.GetValues(headerName); if (values != null)
{ foreach (string str in values)
{ list.Add(str.TrimStart(new char[])); } } return list; } static string GetCanonicalizedResourceVersion2(Uri address, string accountName)
{ StringBuilder builder = new StringBuilder("/"); builder.Append(accountName); builder.Append(address.AbsolutePath); CanonicalizedString str = new CanonicalizedString(builder.ToString()); NameValueCollection values = HttpUtility.ParseQueryString(address.Query); NameValueCollection values2 = new NameValueCollection(); foreach (string str2 in values.Keys)
{ ArrayList list = new ArrayList(values.GetValues(str2)); list.Sort(); StringBuilder builder2 = new StringBuilder(); foreach (object obj2 in list)
{ if (builder2.Length > )
{ builder2.Append(","); } builder2.Append(obj2.ToString()); } values2.Add((str2 == null) ? str2 : str2.ToLowerInvariant(), builder2.ToString()); } ArrayList list2 = new ArrayList(values2.AllKeys); list2.Sort(); foreach (string str3 in list2)
{ StringBuilder builder3 = new StringBuilder(string.Empty); builder3.Append(str3); builder3.Append(":"); builder3.Append(values2[str3]); str.AppendCanonicalizedElement(builder3.ToString()); } return str.Value; } }
}

  

  3.在调试项目的时候,注意修改以下参数:

  const string bloburi = @"https://leidemo.blob.core.chinacloudapi.cn";       //修改为Storage Endpoint

  const string accountname = "leidemo";                       //修改为你的存储账号名称

  const string key = "[YourStorageAccountKey]";                   //修改为你的存储账号Key

  string container = "public";                                //修改为存储账号的Container Name

  4.运行成功,你将会看到Console程序输出如下信息,内容为名为存储账号leidemo,Container为public中所有Blob的信息:

  参考资料:http://blogs.msdn.com/b/azchina/archive/2010/03/19/windows-azure-rest-api-storage-service.aspx

Azure REST API (2) Azure Storage的更多相关文章

  1. Windows Azure Mangement API 之 更方便的使用Mangement API

    许多.Net 程序员在使用Azure Management API的时候都选择参考微软官方示例,通过创建HttpWebRequest来创建. 或者自己创建类库来封装这些API,使之调用起来更加方便. ...

  2. [Windows Azure] .NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5

    .NET Multi-Tier Application Using Storage Tables, Queues, and Blobs - 1 of 5 This tutorial series sh ...

  3. 【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code

    问题描述 使用Azure密钥保管库(Key Vault)来托管存储账号(Storage Account)密钥的示例中,从Github中下载的示例代码在中国区Azure运行时候会遇见各种认证和授权问题, ...

  4. 如何通过Azure Service Management REST API管理Azure服务

    通过本文你将了解: 什么是Azure Service Management REST API 如何获取微软Azure 订阅号 如何获取Azure管理证书 如何调用Azure Service Manag ...

  5. Azure Management API 之 利用 Windows Azure Management Libraries 来控制Azure platform

    在此之前,我曾经发过一篇文章讲叙了如何利用Azure power shell team 提供的class library. 而就在这篇文章发布之后不久,我又发现微软发布了一个preview 版本的Wi ...

  6. C#码农的大数据之路 - 使用Azure Management API创建HDInsight集群

    Azure平台提供了几乎全线产品的API,可以使用第三方工具来进行管理.对于.NET更是提供封装好了的库方便使用C#等语言实现Azure的管理. 我们使用创建HDInsight集群为例来介绍使用C#管 ...

  7. Azure REST API (3) 使用REST API,操作Azure ARM VM

    <Windows Azure Platform 系列文章目录> 笔者之前遇到一个客户,需求是当发生某一个特定条件的时候,对多台Azure ARM VM执行开机或者关机操作,这个时候就需要使 ...

  8. Azure EA (3) 使用Postman访问海外Azure Billing API

    <Windows Azure Platform 系列文章目录> 本文介绍的是海外版的Azure Global服务,因为跨境内境外网络,访问速度会比较慢 在开始使用Azure Billing ...

  9. Azure EA (2) 使用Postman访问国内Azure Billing API

    <Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China 请读者先看一下之前的文档内容:Azure EA (1) 查看国内Az ...

随机推荐

  1. Java开发视频网站大概需要多少钱?

    这个还真不好说,需要看你对视频网站有什么要求?你的数据库选择的是什么型号的?需要开发几个页面?服务器是需要高端的还是中低端的?还有你对完成时间有什么要求,这些细节也是决定价格的关键因素. 上面这些因素 ...

  2. 解剖SQLSERVER 第八篇 OrcaMDF 现在支持多数据文件的数据库(译)

    解剖SQLSERVER 第八篇  OrcaMDF 现在支持多数据文件的数据库(译) http://improve.dk/orcamdf-now-supports-databases-with-mult ...

  3. 【视频教程】使用UIAutomation开发软件外挂

    UIAutomation是.Net 3.5之后提供的“界面自动化测试”技术,本来是给测试人员用的,不过UIAutomation由于也是界面自动操作的技术,比直接使用keybd_event.GetWin ...

  4. nsq初探

    一. 安装 参考:http://nsq.io/deployment/installing.htmlhttp://www.baiyuxiong.com/?p=873    (推荐.) 不推荐直接把官方的 ...

  5. [.net 面向对象编程基础] (6) 基础中的基础——运算符和表达式

    [.net 面向对象编程基础] (6) 基础中的基础——运算符和表达式 说起C#运算符和表达式,小伙伴们肯定以为很简单,其实要用好表达式,不是一件容易的事.一个好的表达式可以让你做事半功倍的效果,比如 ...

  6. js模版引擎handlebars.js实用教程——each嵌套

    <!DOCTYPE html> <html> <head> <META http-equiv=Content-Type content="text/ ...

  7. 《30天自制操作系统》笔记(03)——使用Vmware

    <30天自制操作系统>笔记(03)——使用Vmware 进度回顾 在上一篇,实现了用IPL加载OS程序到内存,然后JMP到OS程序这一功能:并且总结出下一步的OS开发结构.但是遇到了真机测 ...

  8. 实战使用Axure设计App,使用WebStorm开发(3) – 构建页面架构

    系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求  实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目   实战使 ...

  9. springmvc下实现登录验证码功能

    总体思路,简单讲,就是后台生成图片同时将图片信息保存在session,前端显示图片,输入验证码信息后提交表单到后台,取出存放在session里的验证码信息,与表单提交的验证码信息核对. 点击验证码图片 ...

  10. IOS Runtime-初识runtime(一)

    苹果公布了runtime的源码,可以从地址下载:http://www.opensource.apple.com/tarballs/objc4/ object-c程序在启动的时候,需要一个准备时间,这个 ...