A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/Azure-Samples/azure-functions-cosmosdb-sdk-bindings-python below:

GitHub - Azure-Samples/azure-functions-cosmosdb-sdk-bindings-python

name description languages products page_type urlFragment

Cosmos DB SDK-Type Bindings with Azure Functions (Python)

Bind to rich SDK types when using Cosmos input.

python

azure

azure-functions

azure-cosmos

sdk-bindings

sample

cosmosdb-sdk-type-bindings-with-azure-functions

Cosmos DB SDK-Type Bindings with Azure Functions (Python)

This sample demonstrates how to use the Azure Functions Cosmos DB SDK-type bindings in Python. The supported SDK types includes CosmosClient, DatabaseProxy, and ContainerProxy.

You can learn more about SDK-type bindings for Cosmos DB in the SDK-type Bindings for Python Reference.

Before running the sample, you need the following:

  1. Azure Subscription: An Azure account is required.

  2. Azure Functions Core Tools: Install Azure Functions Core Tools to run and test functions locally.

  3. Python 3.x: Ensure Python 3.9 or later is installed on your machine.

  4. Azure Storage Account: Create a storage account via the Azure Portal and get the connection string.

Using SDK-type Bindings for Cosmos DB in an Azure Function App

The code in the sample folder has already been updated to support use of SDK-type bindings for Cosmos DB. Let's walk through the changed files.

The requirements.txt file has an additional dependency of the azurefunctions-extensions-bindings-cosmosdb module:

azure-functions
azurefunctions-extensions-bindings-cosmosdb

Each cosmosdb_samples_* folder contains function_app.py which imports the azurefunctions-extensions-bindings-cosmosdb module.

import azure.functions as func
import azurefunctions.extensions.bindings.cosmosdb as cosmosdb

In each function_app.py file, there is a function that is an HTTP trigger and Cosmos DB input. This function specifies an arg named client and defines the type as an SDK-type.

The cosmosdb_samples_cosmosclient directory shows the type defined as CosmosClient.

@app.cosmos_db_input(arg_name="client",
                     connection="CosmosDBConnection",
                     database_name=None,
                     container_name=None)
def get_docs(req: func.HttpRequest, client: cosmos.CosmosClient):

The cosmosdb_samples_databaseproxy directory shows the type defined as DatabaseProxy, and the cosmosdb_samples_containerproxy directory shows the type defined as ContainerProxy.

  1. Clone the repository:
        git clone https://github.com/Azure-Samples/azure-functions-cosmosdb-sdk-bindings-python.git
    
  2. Navigate to the project directory:
        cd cosmosdb_samples_cosmosclient
    
  3. Create a Python virtual environment and activate it.
  4. Install the required dependencies:
        pip install -r requirements.txt
    
  5. Update local.settings.json: replace CosmosDBConnection with your Cosmos DB connection string.
  6. Update the database and/or container name (if needed): in function_app.py, some Cosmos SDK types require a database name and/or a container name. Replace depending on the SDK type used that reflects that is in your Cosmos DB instance. For example, using ContainerProxy will require a valid database name and container name.
  7. 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:
                get_docs:  http://localhost:7071/api/cosmos
    
  8. Execute the function:
       Found database with ID: ...
    

There are three main ways to deploy this to Azure:

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 Cosmos DB library.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4