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 ...
随机推荐
- SQL入门经典(七) 之脚本和批处理
什么是脚本.我们前面学的CREATE TABLE <table name> ,USE <database name>这些都是脚本,为什么用脚本.脚本存储到文件中并且可以重复利用 ...
- Amazon AWS EC2开启Web服务器配置
在Amazon AWS EC2申请了一年的免费使用权,安装了CentOS + Mono + Jexus环境做一个Web Server使用. 在上述系统安装好之后,把TCP 80端口开启(iptable ...
- 负载均衡算法(四)IP Hash负载均衡算法
/// <summary> /// IP Hash负载均衡算法 /// </summary> public static class IpHash { static Dicti ...
- 【T-SQL基础】03.子查询
以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化. 本系列[T-SQL基础]主要是针对T-SQL基础的总结. [T-SQL基础]01.单表查询-几道sql查询题 ...
- 解如下方程(java实现)
n (m=1) f(m,n)= m (n=1) f(m-1,n)+f(m,n-1) ...
- 利用html 5 websocket做个山寨版web聊天室(手写C#服务器)
在之前的博客中提到过看到html5 的websocket后很感兴趣,终于可以摆脱长轮询(websocket之前的实现方式可以看看Developer Works上的一篇文章,有简单提到,同时也说了web ...
- Linux网络编程系列-套接口选项控制
获取和设置套接口选项的方法有: getsockopt/setsockopt fcntl ioctl getsockopt/setsockopt 这两个函数仅用于套接口(socket)的设置,另外两个函 ...
- easy-ui 小白进阶史(二):操作数据,easy-ui操作
easy-ui的操作及交互: Html: @using LangBo.Facade; @using LangBo.DataDefine; @using System.Threading.Tasks; ...
- Java-继承,多态-0922-04
定义类Human,具有若干属性和功能:定义其子类Man.Woman: 在主类Test中分别创建子类.父类和上转型对象,并测试其特性. 父类: package com.lianxi3; public c ...
- 项目管理师prince2
项目管理师prince2 PRINCE2并不适合用于管理商业活动中的日常事物.商业日常事务通常是指组织机构日常运营中需要完成的那些工作.例如,公司it系统的维护,宾馆的房间整理,或者运营公司的客户呼叫 ...