Streaming a file into Azure Storage via Azure Web Apps and ASP.NET Web API

Streaming a file into Azure Storage via Azure Web Apps and ASP.NET Web API
TL;DR

Get a Memory stream of the file from HTTP Request Context, then use the Azure Storage Async APIs to stream it into the cloud.

The Code

ASP.Net Web API doesn't have the same upload mechamism that ASP.Net MVC does but you can get a very similar experience by using the MultipartMemoryStreamProvider and grabbing any file upload via a Form Post to the WebAPI.

public async Task UploadFileAsync()
{
    var provider = new MultipartMemoryStreamProvider();
    await this.Request.Content.ReadAsMultipartAsync(provider);

    //stream the file into azure storage
    this.storageProvider.Configure($"production{id}");
    var stream = await provider.Contents[0].ReadAsStreamAsync();
    await this.storageProvider.AddFileAsync(stream, model.Name);
}

The method the storageprovider calls then is below, tied into the upload from stream method available from the Azure Storage Blob API.

public async Task AddFileAsync(Stream inputStream, string destintation)
{
    CheckConfigured();

    var blob = this.blobContainer.GetBlockBlobReference(destintation);

    blob.DeleteIfExists();

    await blob.UploadFromStreamAsync(inputStream);
}