Simplest c# sample to call azure storage api

Simplest c# sample to call azure storage api

2018/10/27 Azure Programming 1

Finally called the azure storage api, stucked for one hour in library. There is a / in + “/{0}/\ncomp:list”, Account);

Remember two things:
1. If you are calling to azure management api, you need a cert. Read https://docs.microsoft.com/en-us/azure/cloud-services/cloud-services-certs-create and https://docs.microsoft.com/en-us/azure/cloud-services/cloud-services-configure-ssl-certificate-portal
2. If you are calling azure storage api, you just need a key, no cert if needed.

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace AzureStorageRestFulAPI
{
class Program
{
static void Main(string[] args)
{
ListContainers();
Console.Read();
}

private static String SignThis(string StringToSign, string Key, string Account)
{

String signature = string.Empty;
byte[] unicodeKey = Convert.FromBase64String(Key);
using (HMACSHA256 hmacSha256 = new HMACSHA256(unicodeKey))
{
Byte[] dataToHmac = System.Text.Encoding.UTF8.GetBytes(StringToSign);
signature = Convert.ToBase64String(hmacSha256.ComputeHash(dataToHmac));
}

String authorizationHeader = String.Format(
System.Globalization.CultureInfo.InvariantCulture,
"{0} {1}:{2}",
"SharedKey",
Account,
signature);
Console.WriteLine("\n authorizationHeader " + authorizationHeader);
return authorizationHeader;
}
static void ListContainers()
{
string Key = @"sGQyidifasdasdD4bHbvPO6UohIEl2BCQWdFLBID91gKNwxMjbmLtsE4DnpxZs6QKNQl//vPEFrg==";
string Account = @"quantrstorage";
DateTime dt = DateTime.UtcNow;
string StringToSign = String.Format("GET\n"
+ "\n" // content encoding
+ "\n" // content language
+ "\n" // content length
+ "\n" // content md5
+ "\n" // content type
+ "\n" // date
+ "\n" // if modified since
+ "\n" // if match
+ "\n" // if none match
+ "\n" // if unmodified since
+ "\n" // range
+ "x-ms-date:" + dt.ToString("R") + "\n" // headers
+ "x-ms-version:2014-02-14\n"
+ "/{0}/\ncomp:list", Account);

Console.WriteLine("\n StringToSign " + StringToSign);
string auth = SignThis(StringToSign, Key, Account);
string method = "GET";
string urlPath = string.Format("https://{0}.blob.core.windows.net/?comp=list", Account);
Uri uri = new Uri(urlPath);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = method;
request.Headers.Add("Authorization", auth);
request.Headers.Add("x-ms-date", dt.ToString("R"));
request.Headers.Add("x-ms-version", "2014-02-14");
Console.WriteLine("auth=" + auth);

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string text = reader.ReadToEnd();
Console.WriteLine("\n responseSTtr " + text);
}
}
}
}
}

read count : 7

One Response

  1. daxi says:

    学无止境,认真拜读!

Leave a Reply

Your email address will not be published. Required fields are marked *