以下单元测试失败:
[TestMethod]
public void Add_file_to_blob_and_retrieve_it()
{
var blobName = Guid.NewGuid().ToString();
var testFileContents = File.ReadAllText(TestFileSpec);
Trace.WriteLine(string.Format("Opening blob container {0}", UnitTestBlobAgentName));
CloudStorageAccount.SetConfigurationSettingPublisher(
(configName, configSetter) => configSetter(ConfigurationManager.AppSettings[configName]));
var cloudStorage = CloudStorageAccount.FromConfigurationSetting("StorageConnectionString");
var blobClient = cloudStorage.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(UnitTestBlobAgentName.ToLower());
try
{
Trace.WriteLine(string.Format("Uploading file {0}", TestFileSpec));
var blob = container.GetBlobReference(blobName);
blob.UploadFile(TestFileSpec);
blob.Properties.ContentType = "ByteArray";
blob.SetProperties();
var blob1 = container.GetBlobReference(blobName);
var found = blob1.DownloadText();
Assert.AreEqual(testFileContents.Trim(), found.Trim());
}
finally
{
if (null != container)
{
Trace.WriteLine(string.Format("Deleting blob {0}", blobName));
var blob2 = container.GetBlobReference(blobName);
blob2.DeleteIfExists(new BlobRequestOptions { DeleteSnapshotsOption = DeleteSnapshotsOption.IncludeSnapshots });
}
}
}
事实证明,返回的字符串以dword 0xFEFF
(Unicode BOM)开头。我已经跟踪了Microsoft调试符号,并且BOM存在于返回流中。 AFAICT,它来自HttpResponse.GetResponseStream()
类中的Microsoft.WindowsAzure.StorageClient.CloudBlob
方法调用方式。
确保输入和输出完全相同的最佳方法是什么?在进入之前确保输入转换为Unicode?从输出中剥离BOM?还有其他想法吗?
答案 0 :(得分:1)
这是一个旧的,但如果您的blob在azure中编码为Unicode,并且您想将其下载到文本字符串,则此代码将起作用。请记住,这里的弱点是你必须分配两次内存。如果有一种更有效的方法来获取Unicode字符串(无论如何同步),我找不到它。
string fileText;
using (var memoryStream = new MemoryStream())
{
cloudBlob.DownloadToStream(memoryStream);
memoryStream.Position = 0;
using (var reader = new StreamReader(memoryStream, Encoding.Unicode))
{
fileText = reader.ReadToEnd();
}
}