CloudBlob.DownloadText方法是否插入其他字符? [英] CloudBlob.DownloadText method inserts additional character?

查看:70
本文介绍了CloudBlob.DownloadText方法是否插入其他字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下单元测试失败:

[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,它来自Microsoft.WindowsAzure.StorageClient.CloudBlob类中的HttpResponse.GetResponseStream()方法调用.

It turns out, the returned string begins with the dword 0xFEFF (the Unicode BOM). I've traced through the Microsoft debug symbols, and the BOM exists in the return stream. AFAICT, it comes from the HttpResponse.GetResponseStream() method call way down in the Microsoft.WindowsAzure.StorageClient.CloudBlob class.

确保输入和输出相同的最佳方法是什么?确保在输入之前将输入转换为Unicode?从输出中剥离物料清单?还有其他想法吗?

What's the best way to ensure that the input and output are identical? Ensure the input is converted to Unicode before going in? Strip the BOM from the output? Any other ideas?

推荐答案

这是一个较旧的版本,但是如果您的blob在azure中被编码为Unicode,并且您想将其下载到文本字符串中,则此代码将诡计.请记住,这里的缺点是您必须分配两次内存.如果有一种更有效的方式来获取Unicode字符串(无论如何同步),我找不到它.

This is an old one, but if your blob is encoded as Unicode in azure, and you want to download it to a text string, this code will do the trick. Just keep in mind, the weakness here is that you've got to allocate the memory twice. If there's a more efficient way of getting to a Unicode string (synchronously, anyway,) I couldn't find it.

string fileText;
using (var memoryStream = new MemoryStream())
{
    cloudBlob.DownloadToStream(memoryStream);

    memoryStream.Position = 0;

    using (var reader = new StreamReader(memoryStream, Encoding.Unicode))
    {
        fileText = reader.ReadToEnd();
    }
}

这篇关于CloudBlob.DownloadText方法是否插入其他字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆