c# - 上传文件时出现 azure blob 存储编码问题

标签 c# .net azure azure-blob-storage

我正在使用指定编码 iso-8859-1 的 .Net 包将文件上传到 Azure Blob 存储。该流在内存中看起来没问题,但是当我上传到 blob 存储时,它以损坏的字符结尾,这些字符似乎无法转换为该编码。似乎该文件以损坏的状态存储,当我再次下载并检查它时,字符变得一团糟。这是我正在使用的代码。

    public static async Task<bool> UploadFileFromStream(this CloudStorageAccount account, string containerName, string destBlobPath, string fileName, Stream stream, Encoding encoding)
    {
        if (account is null) throw new ArgumentNullException(nameof(account));
        if (string.IsNullOrEmpty(containerName)) throw new ArgumentException("message", nameof(containerName));
        if (string.IsNullOrEmpty(destBlobPath)) throw new ArgumentException("message", nameof(destBlobPath));
        if (stream is null) throw new ArgumentNullException(nameof(stream));
        stream.Position = 0;
        CloudBlockBlob blob = GetBlob(account, containerName, $"{destBlobPath}/{fileName}");
        blob.Properties.ContentType = FileUtils.GetFileContentType(fileName);
        using var reader = new StreamReader(stream, encoding);
        var ct = await reader.ReadToEndAsync();
        await blob.UploadTextAsync(ct, encoding ?? Encoding.UTF8, AccessCondition.GenerateEmptyCondition(), new BlobRequestOptions(), new OperationContext());
        return true;
    }

这是上传之前的文件

<provinciaDatosInmueble>Sevilla</provinciaDatosInmueble>
<inePoblacionDatosInmueble>969</inePoblacionDatosInmueble>
<poblacionDatosInmueble>Valencina de la Concepción</poblacionDatosInmueble>

这是上传后的文件

<provinciaDatosInmueble>Sevilla</provinciaDatosInmueble>
<inePoblacionDatosInmueble>969</inePoblacionDatosInmueble>
<poblacionDatosInmueble>Valencina de la Concepci�n</poblacionDatosInmueble>

我发送的编码在编码参数中是ISO-5589-1。有人知道为什么 Blob 存储似乎忽略我指定的编码吗?提前致谢!

最佳答案

我们可以使用Azure.Storage.Blobs来实现这一点而不是WindowsAzure.Storage这是一个传统的存储 SDK。下面是对我们有用的代码。

class Program
    {
        static async Task Main(string[] args)
        {

            string sourceContainerName = "<Source_Container_Name>";
            string destBlobPath = "<Destination_Path>";
            string fileName = "<Source_File_name>";

            MemoryStream stream = new MemoryStream();
            BlobServiceClient blobServiceClient = new BlobServiceClient("<Your_Connection_String>");
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient(sourceContainerName);
            BlobClient blobClientSource = containerClient.GetBlobClient(fileName);
            BlobClient blobClientDestination = containerClient.GetBlobClient(destBlobPath);

            // Reading From Blob
            var line =" ";
            if (await blobClientSource.ExistsAsync())
            {
                var response = await blobClientSource.DownloadAsync();

                using (StreamReader streamReader = new StreamReader(response.Value.Content))
                {
                    line = await streamReader.ReadToEndAsync();
                }
            }
            
            // Writing To Blob
            var content = Encoding.UTF8.GetBytes(line);
            using (var ms = new MemoryStream(content))
                blobClientDestination.Upload(ms);
        }
    }

结果:

enter image description here

关于c# - 上传文件时出现 azure blob 存储编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71998445/

相关文章:

azure - 经常请求项目的缓存模型

c# - Azure.RequestFailedException : 'Service request failed. 状态 : 404 (The specified blob does not exist. )错误代码:BlobNotFound

c# - 如何正确重试 HttpRequestMessage?

.net - GAC(64位系统)比较?

c# - 在 ASP.NET MVC 2 中使用 SelectList 挣扎

.net - 集成测试 ASP.NET Core 与 .NET Framework - 找不到 deps.json

.net - 我遗漏了一些关于哈希值的有用性

azure - 如何在ARM模板中多次运行相同的脚本?

c# - 使用无人值守的用户/密码创建 Microsoft GraphServiceClient

c# - 从 dll 调用 opencv Mat 到 Windows 窗体,图像出现故障