2016-05-14 39 views
0

嘗試將字節附加到Azure存儲中的附加Blob時,出現HTTP 400錯誤請求。 Azure存儲帳戶已存在。HTTP 400:附加到Azure Blob時出現錯誤請求

Azure文檔只有一個示例調用AppendText()方法,但此方法包含與多客戶端編寫器一起使用此方法的警告。因此,我想使用AppendBlock()方法來確保調用是原子的。

var cred = new StorageCredentials("storageaccountname", "storageaccountkey"); 
var account = new CloudStorageAccount(cred, true); 

var client = account.CreateCloudBlobClient(); 
var container = client.GetContainerReference("csharp"); 
container.CreateIfNotExists(); 

var log = container.GetAppendBlobReference("artofshell.log"); 
var stream = new MemoryStream(1024); 
var text = System.Text.Encoding.ASCII.GetBytes("Log entry #1"); 
stream.Write(text, 0, text.Length); 

log.CreateOrReplace(); 
log.AppendBlock(stream); 

任何想法可能導致這種情況?

  • 的Windows 10企業內幕建設14332
  • 的Visual Studio 2015年
  • WindowsAzure.Storage庫7.0.1預覽

回答

1

要解決此問題,請重置流的位置只調用AppendBlock()前。所以,你的代碼將是:

 log.CreateOrReplace(); 
     stream.Position = 0;//Reset stream's position 
     log.AppendBlock(stream); 

你得到這個錯誤的原因是因爲你想發送0字節。由於流位於最後,因此您嘗試發送的內容長度爲0,存儲服務不喜歡它:)。當我跑到你的代碼和提琴手跟蹤的請求/響應,我400錯誤以及與以下細節:

<?xml version="1.0" encoding="utf-8"?> 
    <Error> 
     <Code>InvalidHeaderValue</Code> 
     <Message> 
      The value for one of the HTTP headers is not in the correct format. 
      RequestId:b265553a-0001-0072-6e58-ae8e34000000 
      Time:2016-05-15T03:15:51.0349150Z 
     </Message> 
     <HeaderName> 
      Content-Length 
     </HeaderName> 
     <HeaderValue> 
      0 
     </HeaderValue> 
    </Error> 
+0

存儲模擬器還不支持這個權利? – Sushant

相關問題