Reading and writing binary files with Python Azure Functions input and output bindings
Tags: azure, cloud, serverless, python
Published: 2020-11-09 22:02:21

---
title: Reading and writing binary files with Python Azure Functions input and output bindings
published: true
date: 2020-10-29 00:02:59 UTC
tags: Azure,Cloud,Serverless,Python
canonical_url: https://blog.siliconvalve.com/2020/10/29/reading-and-writing-binary-files-with-python-with-azure-functions-input-and-output-bindings/
---

Ah, Streams… how could I ever forget about manipulating files in code and needing to use Streams to read and write them!

I’ve written a lot of .NET in my life and am used to the way you read and write files with it, but I’ve been increasingly doing a lot of Python (BTW: .NET could learn a lot from Python’s [Virtual Environments](https://docs.python.org/3/tutorial/venv.html) construct!) and have been having to learn a whole new set of constructs to work with elements such as files.

For an upcoming demo I developed a small [Python Azure Function](https://docs.microsoft.com/azure/azure-functions/functions-reference-python) whose job it is to create a thumbnail image of a JPEG file. You can find the source for this Azure Function [on GitHub](https://github.com/sjwaight/AzureFunctionImagePythonThumbnailer).

### Azure Functions Triggers and Bindings

If you aren’t familiar with Azure Functions let me briefly cover a couple of key concepts – [Triggers and Bindings](https://docs.microsoft.com/azure/azure-functions/functions-triggers-bindings?tabs=python).

Azure Functions provide developers with a rapid development environment for event-driven solutions by having pre-built logic that removes a lot of plumbing code from your codebase.

Triggers are how an Azure Function is invoked and can be from many different sources – Timers triggers (like cron on Linux), File (Blob) Created Trigger, et al. You define the Trigger type when creating the Function and the Azure Functions tooling automatically wires up the Trigger, gives you configuration options and adds some parameters to the entry point of your Function that allow you to access context from the Trigger source.

In the sample Function below we have a Blob Trigger that includes a filter on the file type (JPG) that will cause the Trigger to fire. This Trigger also provides some additional parameters we can access – a Stream that contains the binary data of the file along with the “name” of the file which in this case just maps to the filename without the extension.

{% gist https://gist.github.com/sjwaight/d04554b64ca88e9781004315a35baf83 %}

This sample Function also uses an Output Binding. You can see the Output Binding definition in the above sample on line 13 where the [return] attribute defines the target for the Output (in this case an Azure Storage Queue).

C# Class Library Functions behave a little differently to other languages because we can be define a Binding entirely in code and not have to configure it via a function.json file.

### My Scenario

I wanted my Python Azure Function to receive a message from an [Azure Storage Queue](https://docs.microsoft.com/azure/storage/queues/storage-queues-introduction), where the message contains the name of a file (blob) that has been uploaded previously to an Azure Blob Storage Container. The file would be downloaded to the Function host, processed and then written back to Azure Blob Storage at a different location.

The queue message, which is part of the Trigger for the Function, is bound via configuration to the Input and Output bindings in the function.json as follows. The special formatting of {queueTrigger} means at runtime the Functions handler will pass the message content to the Input and Output Binding automatically which means as a developer I don’t need to write anything up in my code to achieve this.

{% gist https://gist.github.com/sjwaight/3a2e35aad7f164b504f87bc33cfea3a1 %}

This configuration is actually based on the [Python Azure Functions documentation](https://docs.microsoft.com/azure/azure-functions/functions-bindings-storage-blob-output?tabs=python) which is great for understanding the general format to use to create the bindings, but the sample Function they have is very basic and doesn’t explore how you might manipulate the incoming file and write a file to the output binding.

### The Solution

As it turns out the solution isn’t that difficult, and I suspect some of my challenge was me trying to understand how to work with file streams in Python.

The resulting Function code (see the [full solution on GitHub](https://github.com/sjwaight/AzureFunctionImagePythonThumbnailer/blob/main/ImageThumbnailProcessor/ __init__.py)) is shown below. You can match the ‘inputblob’ and ‘outputblob’ parameters to the function.json configuration shown above. This really demonstrates how clean your code can be when working with Bindings.

{% gist https://gist.github.com/sjwaight/0a51202519a6fd1bea0483257f2cb227 %}

Lines 20 and 32 are the two key ones. On line 20 we receive the bytes from the input binding and invoke the read() Python method which will read in all bytes to the end of the stream. This is done inside of the write() method for our new local file object. As it turns out this is super clean!

The output binding (line 32) is similarly clean – we call outputblob.set() to write the file data to that stream and use the local file’s read() method to read the bytes from the new local file.

**Note:** there is one minor detail to be aware of. Right now there is a limitation with the Output Binding for Azure Blob Storage – all files are written with a Content Type of ‘application/octet-stream’ which means you can’t easily embed them in web pages (you will be prompted to download the file). There is an [open GitHub Issue](https://github.com/Azure/azure-functions-host/issues/364) for this limitation, so hopefully it will be something we can control in future. If you’re not intending to serve the files via the web directly then this is a non-issue.

So there we are – how you can quickly and cleanly use Azure Functions written in Python to process files. Hopefully this post will save you some time if you’re looking to do something similar in your code!

Happy Days 😎