Bushuev
Bushuev

Reputation: 577

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 ?

Upvotes: 4

Views: 2491

Answers (2)

Jason Zimmer
Jason Zimmer

Reputation: 21

I had the same issue, set the position of the memory stream to 0 before calling UploadFromStream.

Upvotes: 0

usr
usr

Reputation: 171206

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

Upvotes: 13

Related Questions