0

I'm uploading files to Azure Blob Storage with the .Net package specifying the encoding iso-8859-1. The stream seems ok in Memory but when I upload to the blob storage it ends with corrupted characters that seems that could not be converted to that encoding. It would seem as if the file gets storaged in a corrupted state and when I download it again and check it the characters get all messed up. Here is the code I'm using.

    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;
    }

This is the file just before uploading it

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

and this is the file after the upload

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

The encoding I send is ISO-5589-1 in the parameter of the encoding. Anybody knows why Blob Storage seems to ignore the encoding I'm specifying? Thanks in advance!

3
  • FYI - Please do not upload images of code/data/errors when asking a question. Commented Apr 25, 2022 at 14:57
  • @DavidMakogon edited, but I don't think it was any code/data/error relevant for the technical point in this question, those are just names, text plain. Commented Apr 25, 2022 at 16:11
  • It's relevant for anyone who wants to copy/paste your data, use a screen reader, etc. Not everyone can read (or use) an image of text. Commented Apr 25, 2022 at 16:12

1 Answer 1

1

We could able to achieve this using Azure.Storage.Blobs instead of WindowsAzure.Storage which is a legacy Storage SDK. Below is the code that worked for us.

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);
        }
    }

RESULT:

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

FYI my comment to the OP is just as relevant here: Please edit to include properly-formatted text, not an image of text. See Please do not upload images of code/data/errors when asking a question.
Aside from that, please edit your question to explain why/how your answer differs from what the OP originally did.
@DavidMakogon The picture I have uploaded is just to show how it appears in a storage account and to show that it is working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.