2012-04-23 39 views
5

我使用PutBlock和PutBlockList將數據上傳到塊斑點,我使用這個代碼如下: -寫作天青阻止斑點

CloudBlobContainer container = blobStorage.GetContainerReference("devicebackups"); 
var permissions = container.GetPermissions(); 
permissions.PublicAccess = BlobContainerPublicAccessType.Container; 
container.SetPermissions(permissions); 
CloudBlockBlob blob = container.GetBlockBlobReference(serialNo.ToLower() + " " + dicMonths[DateTime.Now.Month]); 
try 
{ 
    var serializer = new XmlSerializer(typeof(List<EnergyData>)); 
    var stringBuilder = new StringBuilder(); 
    using (XmlWriter writer = XmlWriter.Create(stringBuilder)) 
    { 
     try 
     { 
      serializer.Serialize(writer, deviceData); 
      byte[] byteArray = Encoding.UTF8.GetBytes(stringBuilder.ToString()); 

      List<string> blockIds = new List<string>(); 
      try 
      { 
       blockIds.AddRange(blob.DownloadBlockList(BlockListingFilter.Committed).Select(b => b.Name)); 
      } 
      catch (StorageClientException e) 
      { 
       if (e.ErrorCode != StorageErrorCode.BlobNotFound) 
       { 
        throw; 
       } 
       blob.Container.CreateIfNotExist(); 
      } 
      var newId = Convert.ToBase64String(Encoding.UTF8.GetBytes(blockIds.Count().ToString())); 
      blob.PutBlock(newId, new MemoryStream(byteArray), null); 
      blockIds.Add(newId); 
      blob.PutBlockList(blockIds); 
     } 
     catch (Exception ex) 
     { 
      UT.ExceptionReporting(ex, "Error in Updating Backup Blob - writing byte array to blob"); 
     } 
    } 
} 
catch (Exception ex) 
{ 
    UT.ExceptionReporting(ex, "Error in Updating Backup Blob - creating XmlWriter"); 
} 
} 
catch (Exception ex) 
{ 
    UT.ExceptionReporting(ex, "Error in Updating Backup Blob - getting container and blob references, serial no -" + serialNo); 
} 

本工程爲10塊,然後在11日阻止它崩潰,並出現以下錯誤: -

StorageClientException - 指定的阻止列表無效。

InnerException = {"The remote server returned an error: (400) Bad Request."} 

我在互聯網上搜索了同樣的錯誤報告,但沒有運氣。

任何幫助將不勝感激。

+1

出於好奇,爲什麼你只是使用一個流將它後臺打印到blob的任何原因? – BrentDaCodeMonkey 2012-04-23 12:44:23

+0

您是在模擬器中還是在真實存儲帳戶中使用此代碼? – 2012-04-23 13:00:52

+0

你錯過了'try {'在開頭,因爲你有一個'} catch',所有代碼都在括號內 – 2012-04-23 13:07:58

回答

14

For a given blob, the length of the value specified for the blockid parameter must be the same size for each block.

http://msdn.microsoft.com/en-us/library/windowsazure/dd135726.aspx

前10塊至9編號爲0的第11塊數是10,其是較長的一個字符。所以你應該改變你的編號方案,使其始終使用相同的長度。一種解決方案是將計數轉換爲足夠長的零填充字符串,以保存您期望擁有的塊數。

但是,如果你不需要使用塊的好處,那麼你可能更好的辦法是一次性寫整個blob,而不是使用塊。

+0

感謝您的答案breischl,我使用轉換後的字符串長度來比較塊ID。我需要塊功能,因爲我沒有在同一時間所有的數據。我會在今天實施這個,讓你知道它是如何發生的。再次感謝。 – ChrisW 2012-04-24 08:34:47

+0

這工作很好,謝謝! – ChrisW 2012-04-24 14:25:54

2

設置你的塊標識有下面的代碼

var blockIdBase64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(blockId.ToString(CultureInfo.InvariantCulture).PadLeft(32, '0'))); 
0

我的問題是,經過10放塊,我收到了錯誤的請求Error 400)。

  1. System.BitConverter.GetBytesstring blockIdBase64=Convert.ToBase64String(System.BitConverter.GetBytes(x++));blockIDs替換Encoding.UTF8.GetBytes必須是相同的大小。 BitConverter.GetBytes是不是 工作。
  2. 我仍收到不良要求(Error 400)。我通過 通過'Azure存儲瀏覽器'刪除​​來解決它。這就像 重置我之前的不良嘗試中的臨時塊。