Skip to content

Commit 58d4ace

Browse files
Copilotnzthiago
andcommitted
Implement SDK type bindings for ContainerClient
Co-authored-by: nzthiago <[email protected]>
1 parent 1438905 commit 58d4ace

File tree

2 files changed

+13
-61
lines changed

2 files changed

+13
-61
lines changed

src/function_app.py

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,20 @@
11
import logging
22
import azure.functions as func
3-
from azure.storage.blob import BlobServiceClient
4-
from azure.identity import DefaultAzureCredential
5-
import os
3+
from azure.storage.blob import ContainerClient
64

75
app = func.FunctionApp()
86

9-
# Initialize clients at module level for reuse across function executions
10-
blob_service_client = None
11-
credential = DefaultAzureCredential()
12-
13-
def get_blob_service_client() -> BlobServiceClient:
14-
"""Get a reusable BlobServiceClient instance."""
15-
global blob_service_client
16-
17-
if not blob_service_client:
18-
# For local development, use the connection string directly
19-
connection_string = os.environ.get('PDFProcessorSTORAGE')
20-
21-
if not connection_string:
22-
logging.error('Storage connection string not found. Expected PDFProcessorSTORAGE__serviceUri environment variable.')
23-
connection_string = os.environ.get('PDFProcessorSTORAGE__serviceUri')
24-
25-
# Check if running locally with Azurite
26-
if connection_string == 'UseDevelopmentStorage=true':
27-
# Use Azurite connection string
28-
blob_service_client = BlobServiceClient.from_connection_string(connection_string)
29-
else:
30-
# Create BlobServiceClient using the storage account URL and managed identity credentials
31-
blob_service_client = BlobServiceClient(
32-
connection_string,
33-
credential
34-
)
35-
36-
return blob_service_client
37-
387
@app.blob_trigger(arg_name="blob",
398
path="unprocessed-pdf/{name}",
409
connection="PDFProcessorSTORAGE",
4110
source="EventGrid")
42-
def process_blob_upload(blob: func.InputStream) -> None:
11+
@app.blob_input(arg_name="processed_container",
12+
path="processed-pdf",
13+
connection="PDFProcessorSTORAGE")
14+
def process_blob_upload(blob: func.InputStream, processed_container: ContainerClient) -> None:
4315
"""
4416
Azure Function triggered by Event Grid when a blob is uploaded to the unprocessed-pdf container.
45-
Processes PDF files and copies them to the processed-pdf container.
17+
Processes PDF files and copies them to the processed-pdf container using SDK type bindings.
4618
"""
4719
blob_name = blob.name.split('/')[-1] # Extract filename from full path
4820
blob_data = blob.read()
@@ -51,32 +23,13 @@ def process_blob_upload(blob: func.InputStream) -> None:
5123
logging.info(f'Python Blob Trigger (using Event Grid) processed blob\n Name: {blob_name} \n Size: {file_size} bytes')
5224

5325
try:
54-
# Copy to processed container - simple demonstration of an async operation
55-
copy_to_processed_container(blob_data, f"processed_{blob_name}")
26+
# Copy to processed container using the bound ContainerClient
27+
processed_blob_name = f"processed_{blob_name}"
28+
blob_client = processed_container.get_blob_client(processed_blob_name)
29+
blob_client.upload_blob(blob_data, overwrite=True)
5630

31+
logging.info(f'Successfully copied {processed_blob_name} to processed-pdf container using SDK type bindings')
5732
logging.info(f'PDF processing complete for {blob_name}')
5833
except Exception as error:
5934
logging.error(f'Error processing blob {blob_name}: {error}')
60-
raise error
61-
62-
def copy_to_processed_container(blob_data: bytes, blob_name: str) -> None:
63-
"""
64-
Simple method to demonstrate uploading the processed PDF to the processed-pdf container.
65-
"""
66-
logging.info(f'Starting copy operation for {blob_name}')
67-
68-
try:
69-
# Get the reusable BlobServiceClient
70-
blob_service_client = get_blob_service_client()
71-
72-
# Get container client for processed PDFs
73-
container_client = blob_service_client.get_container_client('processed-pdf')
74-
75-
# Upload the blob
76-
blob_client = container_client.get_blob_client(blob_name)
77-
blob_client.upload_blob(blob_data, overwrite=True)
78-
79-
logging.info(f'Successfully copied {blob_name} to processed-pdf container')
80-
except Exception as error:
81-
logging.error(f'Failed to copy {blob_name} to processed container: {error}')
8235
raise error

src/requirements.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Azure Functions Worker
22
azure-functions>=1.18.0
33

4-
# Azure SDK packages for blob storage and identity
5-
azure-storage-blob>=12.19.0
6-
azure-identity>=1.15.0
4+
# Azure SDK packages for blob storage
5+
azure-storage-blob>=12.19.0

0 commit comments

Comments
 (0)