[英]Create Table in Azure using REST API
有沒有人嘗試使用REST API在存儲帳戶中創建表? 如果我使用SharedKeyLite授權,我可以毫無問題地執行此操作。 但是,使用SharedKey授權時,身份驗證將繼續失敗。
我的猜測是它必須使用授權簽名中的“Content-MD5”值。 文檔在Content-MD5值上有點模糊,我找不到在文檔中生成Content-MD5的推薦方法。
我找不到使用C#的例子。 我發現的唯一例子是使用Powershell,它使用Content-MD5的空字符串。 但是,這對我的情況不起作用。
這是我的代碼:
public static void CreateTable(string storageAccount, string storageKey, string tableName)
{
var client = new HttpClient();
string date = DateTime.UtcNow.ToString("R", System.Globalization.CultureInfo.InvariantCulture);
client.DefaultRequestHeaders.Add("x-ms-date", date);
string msVersion = "2018-03-28";
client.DefaultRequestHeaders.Add("x-ms-version", msVersion);
client.DefaultRequestHeaders.Add("MaxDataServiceVersion", "3.0;NetFx");
client.DefaultRequestHeaders.Add("DataServiceVersion", "3.0;NetFx");
client.DefaultRequestHeaders.Add("Accept", "application/json;odata=nometadata");
string payload = "{ \"TableName\":\""+ tableName +"\" }";
int contentLength = GetContentLength(payload);
string authH = "SharedKey " + storageAccount + ":" + CreateTableSignature("POST", payload, "application/json", date, storageAccount + "/Tables", storageKey, new List<string>() { });
client.DefaultRequestHeaders.Add("Authorization", authH);
string requestUri = $"https://{storageAccount}.table.core.windows.net/Tables";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUri);
request.Content = new StringContent(payload,
Encoding.UTF8,
"application/json");
request.Content.Headers.ContentLength = contentLength;
client.SendAsync(request)
.ContinueWith(responseTask =>
{
Console.WriteLine("Response: {0}", responseTask.Result);
});
}
public static string CreateTableSignature(string verb, string content, string contentType, string date, string resource, string key, List<string> canonicalizedResourceParms)
{
string msgSignature = verb + "\n" +
CreateMD5(content) + "\n" +
contentType + "\n" +
date + "\n";
msgSignature += "/" + resource;
foreach (string parm in canonicalizedResourceParms)
msgSignature += "\n" + parm;
byte[] SignatureBytes = Encoding.UTF8.GetBytes(msgSignature);
// Create the HMACSHA256 version of the storage key.
HMACSHA256 SHA256 = new HMACSHA256(Convert.FromBase64String(key));
// Compute the hash of the SignatureBytes and convert it to a base64 string.
return Convert.ToBase64String(SHA256.ComputeHash(SignatureBytes));
}
public static string CreateMD5(string input)
{
// Use input string to calculate MD5 hash
using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
{
byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(input);
byte[] hashBytes = md5.ComputeHash(inputBytes);
return Convert.ToBase64String(hashBytes);
}
}
兩點要解決。
內容類型
request.Content = new StringContent(payload, Encoding.UTF8, "application/json");
在這個方法中,SDK實際上設置了application/json; charset=utf-8
發送請求時application/json; charset=utf-8
。 這意味着簽名需要application/json; charset=utf-8
application/json; charset=utf-8
也是如此。
或者我建議您刪除該誤導性內容類型設置並使用
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
內容-MD5
簽名不需要它,你可以把它留空。 否則如果要將其放入簽名中,則需要先添加相應的請求標頭。
request.Content.Headers.ContentMD5 = Convert.FromBase64String(CreateMD5(payload));
此外,在方法CreateMD5()中,編碼應為UTF8
System.Text.Encoding.UTF8.GetBytes(input);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.