Azure REST API (2) Azure Storage
《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的更多相关文章
- Windows Azure Mangement API 之 更方便的使用Mangement API
许多.Net 程序员在使用Azure Management API的时候都选择参考微软官方示例,通过创建HttpWebRequest来创建. 或者自己创建类库来封装这些API,使之调用起来更加方便. ...
- [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 ...
- 【Azure Developer】解决Azure Key Vault管理Storage的示例代码在中国区Azure遇见的各种认证/授权问题 - C# Example Code
问题描述 使用Azure密钥保管库(Key Vault)来托管存储账号(Storage Account)密钥的示例中,从Github中下载的示例代码在中国区Azure运行时候会遇见各种认证和授权问题, ...
- 如何通过Azure Service Management REST API管理Azure服务
通过本文你将了解: 什么是Azure Service Management REST API 如何获取微软Azure 订阅号 如何获取Azure管理证书 如何调用Azure Service Manag ...
- Azure Management API 之 利用 Windows Azure Management Libraries 来控制Azure platform
在此之前,我曾经发过一篇文章讲叙了如何利用Azure power shell team 提供的class library. 而就在这篇文章发布之后不久,我又发现微软发布了一个preview 版本的Wi ...
- C#码农的大数据之路 - 使用Azure Management API创建HDInsight集群
Azure平台提供了几乎全线产品的API,可以使用第三方工具来进行管理.对于.NET更是提供封装好了的库方便使用C#等语言实现Azure的管理. 我们使用创建HDInsight集群为例来介绍使用C#管 ...
- Azure REST API (3) 使用REST API,操作Azure ARM VM
<Windows Azure Platform 系列文章目录> 笔者之前遇到一个客户,需求是当发生某一个特定条件的时候,对多台Azure ARM VM执行开机或者关机操作,这个时候就需要使 ...
- Azure EA (3) 使用Postman访问海外Azure Billing API
<Windows Azure Platform 系列文章目录> 本文介绍的是海外版的Azure Global服务,因为跨境内境外网络,访问速度会比较慢 在开始使用Azure Billing ...
- Azure EA (2) 使用Postman访问国内Azure Billing API
<Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China 请读者先看一下之前的文档内容:Azure EA (1) 查看国内Az ...
随机推荐
- LiveCD DSET日志收集
DELL的LiveCD是一张PE光盘,最新版本7.1是基于CentOS 6.2系统的. 工具下载地址: http://downloads.dell.com/FOLDER01960516M/1/SL ...
- 关于placeholder中 文字添加换行 用转义字符 代替<br>
今天遇到一个问题 UI给的效果图中 文本域的提示文字 是两行显示, 于是就想到placeholder中能否解析html标签, 尝试后发现并无卵用, 经过调查后发现 可以用转义字符代替<br> ...
- Apache HttpAsyncClient 如何设置per request timeout
最近做一个项目时用到HttpAsyncClient:因项目所需,要求能对一个具体的request 设置连接和读写超时:但发现在HttpAsyncClient中,只有在创建一个HttpAsyncClie ...
- oracle 11g install linux
#!/bin/bash#Purpose:Create and config oracle install.#Usage:Log on as the superuser('root') #1.creat ...
- TJ/T808 终端通讯协议设计与实现(码农本色)
由于公司项目涉及到相关技术,对于平常写WEB的技术人员来说对这人来说比较默生:为了让下面的技术人员更好地对这个协议的实施,所以单独针对这个协议进行了分析和设计,以更于后期更好指导相关开发工作.由于自己 ...
- asp.net identity 2.2.0 中角色启用和基本使用(三)
创建控制器 第一步:在controllers文件夹上点右键>添加>控制器, 我这里选的是“MVC5 控制器-空”,名称设置为:RolesAdminController.cs 第二步:添加命 ...
- javascript的执行和预解析
很久以前遇到过一个面试题目,的的确确是面试官问我的问题,下面是这个问题的代码部分.由于年少无知,没有回答上,被无情pass了. var u ='hello world'; ;(function(){ ...
- DDD~我们应该知道的Model,DomainModel和ViewModel
回到目录 图在前 目前项目中可能出现的三种Model模式,对于我们现在开发的一个项目,我觉得使用DDD的思想来设计模型比较清晰,使用DDD的思想把模型model分成了如下三种: 下面是我微博中的截 ...
- windows下配置nginx+php环境
刚看到nginx这个词,我很好奇它的读法(engine x),我的直译是"引擎x",一般引"擎代"表了性能,而"x"大多出现是表示" ...
- Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结
Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 ...