nam
nam

Reputation: 3632

Cannot download blob storage file to memory stream

I am trying to download a file from blob storage into the stream. This code below does two things: download to a file and download to stream, only the download to file part works.

            var connectionString = "..."
            var fileMetaData = await dbService.GetFileMetaDataAsync(fileId);
            var storageAccount = CloudStorageAccount.Parse(connectionString);
            var blobClient = storageAccount.CreateCloudBlobClient();
            var container = blobClient.GetContainerReference(fileMetaData.ProjectCode);
            var blobPath = $"test/test.csv";
            var reference = container.GetBlobReference(blobPath);
            var memoryStream = new MemoryStream();
            try
            {

                await reference.DownloadToStreamAsync(memoryStream);
                await reference.DownloadToFileAsync("C:\\temp\\test.csv", FileMode.Create);
            }
            catch (RequestFailedException)
            {
                logger.LogError($"Cannot download blob at {blobPath}");

                throw;
            }

The Memory stream is empty when I try to read from it with a StreamReader I don't really like the idea of downloading to a temporary file and then read it again Thanks for your help.

Upvotes: 0

Views: 2086

Answers (1)

nam
nam

Reputation: 3632

Using the method OpenReadAsync() solved my problem

var stream = await reference.OpenReadAsync();

Upvotes: 2

Related Questions