Note that there are some explanatory texts on larger screens.

plurals
  1. POStreaming an Azure blob to the client asynchronously with .NET 4.5 async, await
    text
    copied!<p>I'm implementing my own async HTTP handler with <code>HttpTaskAsyncHandler</code> and using async &amp; await to keep it fully asynchronous.</p> <p>I want to stream an azure blob directly to the client. It's true that I can redirect the client to the blob URL and have them download it directly, but let's assume I don't want to do that (my blob is private for example).</p> <p>This is my current implementation:</p> <p><strong>Code:</strong></p> <pre><code> public async Task&lt;bool&gt; DownloadToStreamAsync(ICloudBlob blob, Stream stream) { bool blobFound = true; IAsyncResult asyncResult = blob.BeginDownloadToStream(stream, null, null); await Task.Factory.FromAsync(asyncResult, (r) =&gt; { try { blob.EndDownloadToStream(r); } catch (StorageException) { blobFound = false; } }); return blobFound; } </code></pre> <p><strong>Usage:</strong></p> <pre><code> public override async Task ProcessRequestAsync(HttpContext context) { CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageAccountConnectionString")); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference("container1"); CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1.txt"); await DownloadToStreamAsync(blockBlob, context.Response.OutputStream); } </code></pre> <p>Is this code indeed fully asynchronous and will free my HTTP server to handle other clients while the streaming takes place? (ie if I had a single server thread)</p> <p>Is it possible to keep the blobs compressed (GZIP) and have the client deflate them using <code>Content-Encoding: gzip</code>?</p> <p><strong>Update: (as of Storage 2.1.0.0 RC)</strong></p> <p>Async now supported natively.</p> <pre><code>CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageAccountConnectionString")); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobClient.GetContainerReference("container1"); CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1.txt"); await blockBlob.DownloadToStreamAsync(context.Response.OutputStream); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload