Skip to content

Commit 976b0e2

Browse files
Merge pull request #290339 from ggailey777/ignite-updates
[Functions] Victoria's Python fixes for Event Grid Blob trigger tutorial
2 parents e6a428b + 01597bd commit 976b0e2

File tree

1 file changed

+53
-23
lines changed

1 file changed

+53
-23
lines changed

articles/azure-functions/functions-event-grid-blob-trigger.md

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ When you create a Blob Storage trigger function using Visual Studio Code, you al
6868

6969
Don't choose a project folder that's already part of a workspace.
7070

71-
1. At the prompts, provide the following information:
71+
1. At the prompts, provide the following information:
7272

7373
::: zone pivot="programming-language-csharp"
7474
|Prompt|Action|
@@ -79,7 +79,7 @@ When you create a Blob Storage trigger function using Visual Studio Code, you al
7979
|**Provide a function name**| Enter `EventGridBlobTrigger`. |
8080
|**Provide a namespace** | Enter `My.Functions`. |
8181
|**Select setting from "local.settings.json"**| Select `Create new local app setting`. |
82-
|**Select subscription**| Select your subscription.|
82+
|**Select subscription**| Select your subscription, if needed.|
8383
|**Select a storage account**| Use Azurite emulator for local storage. |
8484
|**The path within your storage account that the trigger will monitor**| Accept the default value `samples-workitems`. |
8585
|**Select how you would like to open your project**| Select `Open in current window`. |
@@ -88,16 +88,16 @@ When you create a Blob Storage trigger function using Visual Studio Code, you al
8888
|Prompt|Action|
8989
|--|--|
9090
|**Select a language**| Select `Python`. |
91-
|**Select a Python programming model** | Select `V1` |
91+
|**Select a Python programming model** | Select `Model V2` |
9292
|**Select a Python interpreter to create a virtual environment**| Select your preferred Python interpreter. If an option isn't shown, enter the full path to your Python binary. |
93-
|**Select a template for your project's first function**| Select `Azure Blob Storage trigger (using Event Grid)`. |
93+
|**Select a template for your project's first function**| Select `Blob trigger`. (The event-based template isn't yet available.)|
9494
|**Provide a function name**| Enter `EventGridBlobTrigger`. |
95+
|**The path within your storage account that the trigger will monitor**| Accept the default value `samples-workitems`. |
9596
|**Select setting from "local.settings.json"**| Select `Create new local app setting`. |
96-
|**Select subscription**| Select your subscription.|
97+
|**Select subscription**| Select your subscription, if needed.|
9798
|**Select a storage account**| Use Azurite emulator for local storage. |
98-
|**The path within your storage account that the trigger will monitor**| Accept the default value `samples-workitems`. |
9999
|**Select how you would like to open your project**| Select `Open in current window`. |
100-
::: zone-end
100+
::: zone-end
101101
::: zone pivot="programming-language-java"
102102
|Prompt|Action|
103103
|--|--|
@@ -112,16 +112,16 @@ When you create a Blob Storage trigger function using Visual Studio Code, you al
112112
|**Select how you would like to open your project**| Select `Open in current window`. |
113113

114114
An HTTP triggered function (`HttpExample`) is created for you. You won't use this function and must instead create a new function.
115-
::: zone-end
116-
::: zone pivot="programming-language-typescript"
115+
::: zone-end
116+
::: zone pivot="programming-language-typescript"
117117
|Prompt|Action|
118118
|--|--|
119119
|**Select a language for your function project**| Select `TypeScript`. |
120120
|**Select a TypeScript programming model**| Select `Model V4`. |
121121
|**Select a template for your project's first function**| Select `Azure Blob Storage trigger (using Event Grid)`. |
122122
|**Provide a function name**| Enter `EventGridBlobTrigger`. |
123123
|**Select setting from "local.settings.json"**| Select `Create new local app setting`. |
124-
|**Select subscription**| Select your subscription.|
124+
|**Select subscription**| Select your subscription, if needed.|
125125
|**Select a storage account**| Use Azurite emulator for local storage. |
126126
|**The path within your storage account that the trigger will monitor**| Accept the default value `samples-workitems`. |
127127
|**Select how you would like to open your project**| Select `Open in current window`. |
@@ -134,7 +134,7 @@ When you create a Blob Storage trigger function using Visual Studio Code, you al
134134
|**Select a template for your project's first function**| Select `Azure Blob Storage trigger (using Event Grid)`. |
135135
|**Provide a function name**| Enter `eventGridBlobTrigger`. |
136136
|**Select setting from "local.settings.json"**| Select `Create new local app setting`. |
137-
|**Select subscription**| Select your subscription.|
137+
|**Select subscription**| Select your subscription, if needed.|
138138
|**Select a storage account**| Use Azurite emulator for local storage. |
139139
|**The path within your storage account that the trigger will monitor**| Accept the default value `samples-workitems`. |
140140
|**Select how you would like to open your project**| Select `Open in current window`. |
@@ -146,7 +146,7 @@ When you create a Blob Storage trigger function using Visual Studio Code, you al
146146
|**Select a template for your project's first function**| Select `Azure Blob Storage trigger (using Event Grid)`. |
147147
|**Provide a function name**| Enter `EventGridBlobTrigger`. |
148148
|**Select setting from "local.settings.json"**| Select `Create new local app setting`. |
149-
|**Select subscription**| Select your subscription.|
149+
|**Select subscription**| Select your subscription, if needed.|
150150
|**Select a storage account**| Use Azurite emulator for local storage. |
151151
|**The path within your storage account that the trigger will monitor**| Accept the default value `samples-workitems`. |
152152
|**Select how you would like to open your project**| Select `Open in current window`. |
@@ -169,8 +169,29 @@ When you create a Blob Storage trigger function using Visual Studio Code, you al
169169

170170
You now have a function that can be triggered by events in a Blob Storage container.
171171

172-
## (Optional) Review the code
172+
::: zone pivot="programming-language-python"
173+
## Update the trigger source
173174

175+
You first need to switch the trigger source from the default Blob trigger source (container polling) to an event subscription source.
176+
177+
1. Open the function_app.py project file and you see a definition for the `EventGridBlobTrigger` function with the `blob_trigger` decorator applied.
178+
179+
1. Update the decorator by adding `source = "EventGrid"`. Your function should now look something like this:
180+
181+
```python
182+
@app.blob_trigger(arg_name="myblob", source="EventGrid", path="samples-workitems",
183+
connection="<STORAGE_ACCOUNT>")
184+
def EventGridBlobTrigger(myblob: func.InputStream):
185+
logging.info(f"Python blob trigger function processed blob"
186+
f"Name: {myblob.name}"
187+
f"Blob Size: {myblob.length} bytes")
188+
```
189+
190+
In this definition `source = "EventGrid"` indicates that an event subscription to the `samples-workitems` blob container is used as the source of the event that starts the trigger.
191+
::: zone-end
192+
::: zone pivot="programming-language-csharp,programming-language-typescript,programming-language-powershell,programming-language-java,programming-language-javascript"
193+
## (Optional) Review the code
194+
::: zone-end
174195
::: zone pivot="programming-language-csharp"
175196
Open the generated `EventGridBlobTrigger.cs` file and you see a definition for an `EventGridBlobTrigger` function that looks something like this:
176197

@@ -195,7 +216,7 @@ Open the generated `EventGridBlobTrigger.java` file and you see a definition for
195216

196217
In this definition `source = EventGrid` indicates that an event subscription to the `samples-workitems` blob container is used as the source of the event that starts the trigger.
197218
::: zone-end
198-
::: zone pivot="programming-language-python,programming-language-powershell"
219+
::: zone pivot="programming-language-powershell"
199220
In the `EventGridBlobTrigger` folder, open the `function.json` file and find a binding definition like this with a `type` of `blobTrigger` and a `source` of `EventGrid`:
200221

201222
:::code language="json" source="~/functions-quickstart-templates/Functions.Templates/Templates/EventGridBlobTrigger-PowerShell/function.json" :::
@@ -348,12 +369,12 @@ Now both the Functions host and the trigger are sharing the same storage account
348369

349370
To create an event subscription, you need to provide Event Grid with the URL of the specific endpoint to report Blob Storage events. This _blob extension_ URL is composed of these parts:
350371

351-
| Part | Example |
352-
| --- | --- |
372+
| Part | Example |
373+
| --- |-------------------------------------------------|
353374
| Base function app URL | `https://<FUNCTION_APP_NAME>.azurewebsites.net` |
354-
| Blob-specific path | `/runtime/webhooks/blobs` |
355-
| Function query string | `?functionName=Host.Functions.EventGridBlobTrigger` |
356-
| Blob extension access key | `&code=<BLOB_EXTENSION_KEY>` |
375+
| Blob-specific path | `/runtime/webhooks/blobs` |
376+
| Function query string | `?functionName=Host.Functions.<FUNCTION_NAME>` |
377+
| Blob extension access key | `&code=<BLOB_EXTENSION_KEY>` |
357378

358379
The blob extension access key is designed to make it more difficult for others to access your blob extension endpoint. To determine your blob extension access key:
359380

@@ -371,7 +392,7 @@ The blob extension access key is designed to make it more difficult for others t
371392
https://<FUNCTION_APP_NAME>.azurewebsites.net/runtime/webhooks/blobs?functionName=Host.Functions.EventGridBlobTrigger&code=<BLOB_EXTENSION_KEY>
372393
```
373394

374-
In this example, replace `<FUNCTION_APP_NAME>` with the name of your function app and replace `<BLOB_EXTENSION_KEY>` with the value you got from the portal. If you used a different name for your function, you'll also need to change the `functionName` query string value to your function name.
395+
In this example, replace `<FUNCTION_APP_NAME>` with the name of your function app, and `<BLOB_EXTENSION_KEY>` with the value you got from the portal. If you used a different name for your function, replace `EventGridBlobTrigger` with that function name.
375396

376397
You can now use this endpoint URL to create an event subscription.
377398

@@ -383,7 +404,7 @@ An event subscription, powered by Azure Event Grid, raises events based on chang
383404

384405
1. Sign in to the [Azure portal](https://portal.azure.com) and make a note of the **Resource group** for your storage account. You create your other resources in the same group to make it easier to clean up resources when you're done.
385406

386-
1. select the **Events** option from the left menu.
407+
1. Select the **Events** option from the left menu.
387408

388409
![Add storage account event](./media/functions-event-grid-blob-trigger/functions-event-grid-local-dev-add-event.png)
389410

@@ -399,8 +420,17 @@ An event subscription, powered by Azure Event Grid, raises events based on chang
399420
| **Endpoint** | Your Azure-based URL endpoint | Use the URL endpoint that you built, which includes the key value. |
400421

401422
1. Select **Confirm selection** to validate the endpoint URL.
423+
2. Select the **Filters** tab and provide the following information to the prompts:
424+
425+
| Setting | Suggested value | Description |
426+
|------------------------------|-----------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
427+
| **Enable subject filtering** | *Enabled* | Enables filtering on which blobs can trigger the function. |
428+
| **Subject Begins With** | **`/blobServices/default/containers/<CONTAINER_NAME>/blobs/<BLOB_PREFIX>`** | Replace `<CONTAINER_NAME` and `<BLOB_PREFIX>` with values you choose. This sets the subscription to trigger only for blobs that start with `BLOB_PREFIX` and are in the `CONTAINER_NAME` container. |
429+
| **Subject Ends With** | *.txt* | Ensures that the function will only be triggered by blobs ending with `.txt`. |
430+
431+
For more information on filtering to specific blobs, see [Event Filtering for Azure Event Hubs](../event-grid/event-filtering.md).
402432

403-
1. Select **Create** to create the event subscription.
433+
7. Select **Create** to create the event subscription.
404434

405435
## Upload a file to the container
406436

@@ -433,6 +463,6 @@ Now that you uploaded a file to the **samples-workitems** container, the functio
433463

434464
## Next steps
435465

436-
+ [Working with blobs](storage-considerations.md#working-with-blobs)
466+
- [Working with blobs](storage-considerations.md#working-with-blobs)
437467
- [Automate resizing uploaded images using Event Grid](../event-grid/resize-images-on-storage-blob-upload-event.md)
438468
- [Event Grid trigger for Azure Functions](./functions-bindings-event-grid.md)

0 commit comments

Comments
 (0)