azure-encryption-extensions icon indicating copy to clipboard operation
azure-encryption-extensions copied to clipboard

Downloading Large Blob Out of Memory

Open lehne opened this issue 9 years ago • 0 comments

I am using the DownloadToStreamEncrypted extension and it works fine for small files. I've noticed that it loads the entire stream into memory however. I am using ASP.Net WebAPI. Here is my code:

var stream = new MemoryStream();
//some code
await blob.DownloadToStreamEncryptedAsync(provider, stream);

stream.Seek(0, SeekOrigin.Begin);

response.StatusCode = HttpStatusCode.OK;

response.Content = new StreamContent(stream, 1024 * 1024);

response.Content.Headers.ContentLength = doc.Length;
response.Content.Headers.ContentType = new MediaTypeHeaderValue(doc.MimeType);
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
    FileName = "somefilename",
    Size = doc.Length
};
return response;

This works, but the memory used goes up by however big the file is. It doesn't actually stream the file back. Is there something I am doing wrong with the stream that is causing this high memory use? Ideally, it just streams back from the server without first getting loaded into memory in its entirety. I suspect the problem is the CopyToAsync method within the DownloadToStreamEncrypted but am not sure.

lehne avatar Sep 16 '16 12:09 lehne