name | description | languages | products | page_type | urlFragment | |||||
---|---|---|---|---|---|---|---|---|---|---|
Blob SDK-Type Bindings with Azure Functions (Python) |
Bind to rich SDK types when using blob trigger or blob input. |
|
|
sample |
blob-sdk-type-bindings-with-azure-functions |
This sample demonstrates how to use the Azure Functions Blob SDK-type bindings in Python. The supported SDK types include BlobClient, ContainerClient, and StorageStreamDownloader.
You can learn more about SDK-type bindings for blob in the SDK-type Bindings for Python Reference.
Before running the sample, you need the following:
-
Azure Subscription: An Azure account is required.
-
Azure Functions Core Tools: Install Azure Functions Core Tools to run and test functions locally.
-
Python 3.x: Ensure Python 3.9 or later is installed on your machine.
-
Azure Storage Account: Create a storage account via the Azure Portal and get the connection string.
The code in the sample folder has already been updated to support use of SDK-type bindings for blob. Let's walk through the changed files.
The requirements.txt
file has an additional dependency of the azurefunctions-extensions-bindings-blob
module:
azure-functions
azurefunctions-extensions-bindings-blob
Each blob_samples_* folder contains function_app.py
which imports the azurefunctions-extensions-bindings-blob
module.
import azure.functions as func
import azurefunctions.extensions.bindings.blob as blob
In each function_app.py
file, there are two functions defined: a blob trigger and a blob input. Both of these functions specify an arg
named client
and define the type as an SDK-type.
The blob_samples_blobclient directory shows the type defined as BlobClient
.
@app.blob_trigger(
arg_name="client", path="PATH/TO/BLOB", connection="AzureWebJobsStorage"
)
def blob_trigger(client: blob.BlobClient):
The blob_samples_containerclient directory shows the type defined as ContainerClient
, and
the blob_samples_storagestreamdownloader directory shows the type defined as StorageStreamDownloader
.
- Clone the repository:
git clone https://github.com/Azure-Samples/azure-functions-blob-sdk-bindings-python.git
- Navigate to one of the project directories:
cd blob_samples_blobclient
- Create a Python virtual environment and activate it.
- Install the required dependencies:
pip install -r requirements.txt
- Update
local.settings.json
: replaceStorageConnection
with your storage account connection string. - Update the blob path: in
function_app.py
, replace PATH/TO/BLOB with your blob path. - Start the function: If you are using VS Code for development, click the "Run and Debug" button or follow the instructions for running a function locally. Outside of VS Code, follow these instructions for using Core Tools commands directly to run the function locally.
Functions: blob_input: http://localhost:7071/api/file blob_trigger: blobTrigger
- Execute the function:
- Blob trigger: upload the specified blob to the storage account. You should see the log below printed in the terminal, where the blob properties and content head are populated for your specific blob.
Python blob trigger function processed blob Properties: {...} Blob content head: ...
- Blob input: ensure the specified blob is uploaded and send a GET request to the function API endpoint.
Python blob input function processed blob Properties: {...} Blob content head: ...
There are three main ways to deploy this to Azure:
- Deploy with the VS Code Azure Functions extension.
- Deploy with the Azure CLI.
- Deploy with the Azure Developer CLI: After installing the
azd
tool, runazd up
in the root of the project. You can also runazd pipeline config
to set up a CI/CD pipeline for deployment.
All approaches will provision a Function App, Storage account (to store the code), and a Log Analytics workspace.
Visit the SDK-type bindings in Python reference documentation to learn more about how to use SDK-type bindings in a Python Function App and the API reference documentation to learn more about what you can do with the Azure Storage Blob client library.