2017-03-08 109 views
1

下面的代碼,當指着一個真正的Azure存儲帳戶將成功返回BLOB內容:如何使用Azure存儲模擬器blob端點獲取blob?

var path = $"{container}/{blob}"; 
var rfcDate = DateTime.UtcNow.ToString("R"); 
var headers = "GET\n\n\n\n\n\n\n\n\n\n\n\n" + 
       "x-ms-blob-type:Block\n" + 
       $"x-ms-date:{rfcDate}\n" + 
       $"x-ms-version:{ServiceVersion}\n" + 
       $"/{AccountName}/{path}"; 
var uri = new Uri(BlobEndpoint + path); 
var request = new HttpRequestMessage(HttpMethod.Get, uri); 
request.Headers.Add("x-ms-blob-type", "Block"); 
request.Headers.Add("x-ms-date", rfcDate); 
request.Headers.Add("x-ms-version", ServiceVersion); 

string signature = ""; 
using (var sha = new HMACSHA256(System.Convert.FromBase64String(AccountKey))) 
{ 
    var data = Encoding.UTF8.GetBytes(headers); 
    signature = System.Convert.ToBase64String(sha.ComputeHash(data)); 
} 

var authHeader = $"SharedKey {AccountName}:{signature}"; 
request.Headers.Add("Authorization", authHeader); 

using (var client = new HttpClient()) 
{ 
    var response = await client.SendAsync(request); 
    return await response.Content.ReadAsStringAsync(); 
} 

但是,如果我將其配置爲使用Azure的模擬器,其中:

  • 帳戶名 = devstoreaccount1
  • AccountKey = Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw ==
  • BlobEndpoint = http://127.0.0.1:10000/
  • ServiceVersion = 2009-09-19

我總是得到404我使用Azure存儲模擬器V4.6。代碼或配置是否不正確,或者仿真器不支持?

回答

0

有兩個問題與您的代碼:

  1. Blob服務中存儲模擬器會偵聽http://127.0.0.1:1000但是基URI http://127.0.0.1:1000/devstoreaccount1
  2. 在計算Signature Stringheader代碼中的變量)時,帳戶名稱必須出現兩次。這是因爲帳戶名稱是資源的URI路徑的一部分(blob的URL爲http://127.0.0.1:1000/devstoreaccount1/container-name/blob-name)。

在此基礎上,請嘗試以下代碼:

static async Task<string> ReadBlobFromDevStorage() 
    { 
     var container = "temp"; 
     var blob = "test.txt"; 
     var ServiceVersion = "2009-09-19"; 
     var AccountName = "devstoreaccount1"; 
     var BlobEndpoint = "http://127.0.0.1:10000/devstoreaccount1"; 
     var path = $"{container}/{blob}"; 
     var AccountKey = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="; 
     var rfcDate = DateTime.UtcNow.ToString("R"); 
     var headers = "GET\n\n\n\n\n\n\n\n\n\n\n\n" + 
         "x-ms-blob-type:Block\n" + 
         $"x-ms-date:{rfcDate}\n" + 
         $"x-ms-version:{ServiceVersion}\n" + 
         $"/{AccountName}/{AccountName}/{path}"; 
     var uri = new Uri(BlobEndpoint + "/" + path); 
     var request = new HttpRequestMessage(HttpMethod.Get, uri); 
     request.Headers.Add("x-ms-blob-type", "Block"); 
     request.Headers.Add("x-ms-date", rfcDate); 
     request.Headers.Add("x-ms-version", ServiceVersion); 

     string signature = ""; 
     using (var sha = new HMACSHA256(System.Convert.FromBase64String(AccountKey))) 
     { 
      var data = Encoding.UTF8.GetBytes(headers); 
      signature = System.Convert.ToBase64String(sha.ComputeHash(data)); 
     } 

     var authHeader = $"SharedKey {AccountName}:{signature}"; 
     request.Headers.Add("Authorization", authHeader); 

     using (var client = new HttpClient()) 
     { 
      var response = await client.SendAsync(request); 
      var content = await response.Content.ReadAsStringAsync(); 
      Console.WriteLine(content); 
      return content; 
     } 
    } 
+0

感謝。我明白我做錯了什麼。 –