Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Azure UploadFromStream does not work if I use MemoryStream (It works but length equals 0)?

I have a class

public class TextCorpusFile
{
    public int Id {get; set;}
    public string FileType {get; set;}
    public MemoryStream File {get; set;}
}

If I try to upload a file to Azure Blob Storage, the length of the file equals 0. ( the file was created, but length is 0)

public void SendTextCorpusFileData(TextCorpusFile textCorpusFile)
{
    //get container by default 
    CloudBlobContainer textCorpusContainer =
        ReturnTextCorpusFileContainer();

    CloudBlockBlob blockBlob = textCorpusContainer.GetBlockBlobReference(textCorpusFile.Id + POINT + textCorpusFile.FileType);

    blockBlob.UploadFromStream(textCorpusFile.File);

}

But if I send files by bytes, it works well and the length is not 0.

public void SendTextCorpusFileData(TextCorpusFile textCorpusFile)
{
    //get by default
    CloudBlobContainer textCorpusContainer =
        ReturnTextCorpusFileContainer();

    CloudBlockBlob blockBlob = textCorpusContainer.GetBlockBlobReference(textCorpusFile.Id + POINT + textCorpusFile.FileType);

    blockBlob.UploadFromByteArray(textCorpusFile.File.ToArray(),
        0,
        (int)textCorpusFile.File.Length);
}

Why it works like this I can not understand (because UploadFromStream(Stream source and I am sure that MemoryStream : Stream)

Can you explain ?

like image 535
Bushuev Avatar asked Dec 11 '22 17:12

Bushuev


1 Answers

Although none of the relevant code is shown I diagnose: The MemoryStream.Position is at the end. This causes reads to return 0 bytes.

like image 132
usr Avatar answered Jan 14 '23 15:01

usr