Azure Python example

Jose 72 Published: 08/13/2024

Azure Python example

I'd be happy to help!

Here is an example of using Azure services with Python:

Azure Storage Blob

from azure.storage.blob import BlobServiceClient
Create a blob service client object

blob_service_client = BlobServiceClient.from_connection_string("DefaultEndpointsProtocol=https;AccountName=<account_name>;AccountKey=<account_key>;BlobEndpoint=<blob_endpoint>")

Get a reference to the container

container_client = blob_service_client.get_container_client("mycontainer")

List all blobs in the container

blobs = list(container_client.list_blobs())

for blob in blobs:

print(blob.name)

Download a specific blob

blob_client = container_client.get_blob_client("myblob")

data = blob_client.download_blob().content_as_text()

print(data)

Azure Cognitive Search

from azure.cognitiveservices.search.services import SearchServiceClient
Create a search client object

search_service_client = SearchServiceClient.from_connection_string("https://<service_name>.search.windows.net;Key=<api_key>")

Perform a search query

results = search_service_client.Documents.query({"query": "mysearchquery"})

for result in results:

print(result.DocumentID)

Create an index

index_client = search_service_client.Indexes.create("myindex", {"fields": ["myfield1", "myfield2"]})

Azure Cosmos DB

from azure.cosmos import CosmosClient
Create a cosmos client object

cosmos_client = CosmosClient("<account_name>", "<account_key>")

Get a reference to the database and container

database_name = "mydatabase"

container_name = "mycontainer"

database = cosmos_client.get_database_client(database_name)

container = database.get_container_client(container_name)

Create a document

document = {"id": 1, "name": "John"}

container.create_item(document)

Query for documents with name matching "John"

query = "SELECT * FROM mycontainer WHERE name = 'John'"

results = list(container.query_items(query, enable_cross_partition_query=True))

for result in results:

print(result)

Azure Function

import logging

from azure.functions import *

@app.function(

name="myfunction",

methods=["POST"],

auth_level=AUTH_LEVEL.ANY,

)

def main(req: func.HttpRequest) -> func.HttpResponse:

Get the request body as JSON

req_body = req.get_json(silent=False)

logging.info(req_body)

Perform some processing on the request body

result = {"processed_data": "myprocesseddata"}

Return a response with the processed data

return func.HttpResponse(result, status_code=200)

These are just a few examples of using Azure services with Python. There are many more services and features available in Azure, so I hope this gives you a good starting point for exploring what's possible!

How to connect Python to Azure?

Here's a detailed guide on how to connect Python to Azure:

Option 1: Using Azure Cosmos DB

Azure Cosmos DB is a globally distributed, multi-model database service that enables you to easily integrate your Python application with Azure. Here's a step-by-step guide:

Create an Azure Cosmos DB account: Log in to the Azure portal and click on "New" > "Data + Storage" > "Cosmos DB". Fill in the required information and click "Create". Install the Azure Cosmos DB Python SDK: You can install the SDK using pip: pip install azure-cosmosdb Connect to your Azure Cosmos DB account: Use the following code snippet to connect to your Azure Cosmos DB account:
from azure.cosmos import CosmosClient

account_name = "your_account_name"

account_key = "your_account_key"

database_name = "your_database_name"

cosmos_client = CosmosClient(account_name, account_key)

database = cosmos_client.get_database_client(database_name)

Replace the placeholders with your actual Azure Cosmos DB account name, key, and database name.

Option 2: Using Azure Blob Storage

Azure Blob Storage is a cloud-based object store that can be used to store and serve large binary files. Here's how you can connect Python to Azure Blob Storage:

Create an Azure Blob Storage container: Log in to the Azure portal and click on "New" > "Data + Storage" > "Blob Storage". Fill in the required information and click "Create". Install the Azure Blob Storage Python SDK: You can install the SDK using pip: pip install azure-storage-blob Connect to your Azure Blob Storage account: Use the following code snippet to connect to your Azure Blob Storage account:
from azure.storage.blob import BlobServiceClient

account_name = "your_account_name"

account_key = "your_account_key"

blob_service_client = BlobServiceClient(account_url=f"https://{account_name}.blob.core.windows.net/", credential=account_key)

Replace the placeholders with your actual Azure Blob Storage account name and key.

Option 3: Using Azure Functions

Azure Functions is a serverless compute service that enables you to run your Python code in response to events. Here's how you can connect Python to Azure Functions:

Create an Azure Functions project: Log in to the Azure portal and click on "New" > "Compute" > "Function App". Fill in the required information and click "Create". Install the Azure Functions Python SDK: You can install the SDK using pip: pip install azure-functions Write your Python code: Use the following code snippet as a starting point for your Azure Function:
from azure.functions import FunctionApp

@FunctionApp(

name="your_function_name",

runtime=runtime_py,

handlers={"*/": "your_function_handler"}

)

def your_function_handler(req):

Your Python code here

pass

Replace the placeholders with your actual Azure Function name, runtime, and handler.

In summary, there are several ways to connect Python to Azure, including using Azure Cosmos DB, Azure Blob Storage, or Azure Functions. Each option has its own set of requirements and steps, but all involve installing a Python SDK and writing code that interacts with the Azure service.