CRUD Operations with Azure Blob Storage in an Azure Function – Read

In the previous post, we examined how we could add an item to the Azure blob.

In this part, we will use an Azure function to download the file store in Azure Blob Storage.

Block Blob

Let us begin with Block Blobs.

[FunctionName("DownloadBlockBlob")]
public static async Task<IActionResult> DownloadBlockBlob(
    [HttpTrigger(AuthorizationLevel.Anonymous,"get",Route =null)] HttpRequest req,
    [Blob("todos")] CloudBlobContainer blobContainer,
    ILogger log)
{
    var key = req.Query["key"];

    var stream = new MemoryStream();
    var blob = await blobContainer.GetBlobReferenceFromServerAsync(key);
    await blob.DownloadToStreamAsync(stream);
    stream.Position = 0;
    return new FileStreamResult(stream, "application/octet-stream") { FileDownloadName = key };
}

This turns out to be pretty straightforward, isn’t it. The CloudBlobContainer.GetBlobReferenceFromServerAsync gets the reference to the blob we are interested in using the blob name, in this case, passed on as a query parameter. We could then use the CloudBlob.DownloadToStreamAsync method to download the contents of the blob to a stream, which could be later pass back using the FileStreamResult.

One obvious question would be why use a azure function in the above case, when there is no authentication. One could directly use a Http Get Request. Partially, that is true, however, do not forget this is only an example and most often than note, authentication would not be anonymous and there could be other things that you would like your azure function to during the download, like a log or updating/fetching another document.

Of course, you could simplify things using input bindings. For example, the above code could be rewritten using binding as the following.

[FunctionName("DownloadBlockBlobUsingBinding")]
public static async Task<IActionResult> DownloadBlockBlobUsingBinding(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "DownloadBlockBlobUsingBinding/{fileName}")] HttpRequest req,
[Blob("todos/{fileName}",FileAccess.Read)] Stream blob,
string fileName,
ILogger log)
{
    var memoryStream = new MemoryStream();
    await blob.CopyToAsync(memoryStream);
    memoryStream.Position = 0;
    return new FileStreamResult(memoryStream, "application/octet-stream") { FileDownloadName = fileName };
}

Append Blob

To download a file from an Append Blob is no different from what we have seen with Block Blob.

[FunctionName("DownloadAppendBlob")]
public static async Task<IActionResult> DownloadAppendBlob(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
[Blob("sample")] CloudBlobContainer blobContainer,
ILogger log)
{

    var key = req.Query["key"];

    var stream = new MemoryStream();
    var blob = blobContainer.GetAppendBlobReference(key);
    await blob.DownloadToStreamAsync(stream);
    stream.Position = 0;
    return new FileStreamResult(stream, "text/plain") { FileDownloadName = key };
}


The only point of importance while learning would be to remember that Append Blobs are not supported by the Storage emulator. For rest of the code look quite similar to the Block blob, sole difference being we use a different method to get reference to the blob – CloudContainer.GetAppendBlobReference.

As mentioned in the earlier post, we will address the Page blob separately. But for now, go ahead and play around with the Block Blob and Append Blob. You could access the source code explained in this code in my Github