Я TChebur
Я TChebur

Reputation: 406

Manage memoryStream when donwload files from Azure Blob

I'm working on creating wrapper for download files from Azure Blob Storage. I have a simple model for file

public class File
{
    public Stream Data { get; set; }
    public string Name { get; set; }
    public string Path { get; set; }
    public long Size { get; set; }
}

my method to download a file looks like this

private async Task<File> GetFile(string name, string path = null)
{
    var blockBlob = _container.GetBlockBlobReference(GetKey(name, path));
    await blockBlob.FetchAttributesAsync();

    var memoryStream = new MemoryStream();
    await blockBlob.DownloadToStreamAsync(memoryStream).ConfigureAwait(false);
    memoryStream.Position = 0;

    return new File(
        name: name,
        path: path: blockBlob.Uri.ToString(),
        data: memoryStream,
        size: blockBlob.Properties.Length);
}

Question: is it ok that I create a memoryStream and never dispose it? Should I pass memoryStream as a parameter and move responsibility for lifetime to caller? If yes, how can I do that with mu model?

Upvotes: 0

Views: 677

Answers (1)

auburg
auburg

Reputation: 1475

MemoryStream doesn't require explicit disposal according to the docs (see note in learn.microsoft.com/en-us/dotnet/api/…) so you're current design is fine.

Upvotes: 1

Related Questions