Skip to content

Commit f19741d

Browse files
Merge pull request #223848 from shreyabatra4/pystein-samples
added more samples
2 parents e2a9ac4 + a76a4e5 commit f19741d

File tree

2 files changed

+131
-9
lines changed

2 files changed

+131
-9
lines changed

articles/azure-functions/functions-bindings-triggers-python.md

Lines changed: 130 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ To create your first function in the new v2 model, see one of these quickstart a
2626
+ [Get started with Visual Studio](./create-first-function-vs-code-python.md)
2727
+ [Get started command prompt](./create-first-function-cli-python.md)
2828

29-
## Blob trigger
29+
## Azure Blob storage trigger
3030

3131
The following code snippet defines a function triggered from Azure Blob Storage:
3232

@@ -36,13 +36,49 @@ import azure.functions as func
3636
app = func.FunctionApp()
3737
@app.function_name(name="BlobTrigger1")
3838
@app.blob_trigger(arg_name="myblob", path="samples-workitems/{name}",
39-
connection="<STORAGE_CONNECTION_SETTING>")
39+
connection="AzureWebJobsStorage")
4040
def test_function(myblob: func.InputStream):
4141
logging.info(f"Python blob trigger function processed blob \n"
4242
f"Name: {myblob.name}\n"
4343
f"Blob Size: {myblob.length} bytes")
4444
```
4545

46+
## Azure Blob storage input binding
47+
48+
```python
49+
import logging
50+
import azure.functions as func
51+
app = func.FunctionApp()
52+
@app.function_name(name="BlobInput1")
53+
@app.route(route="file")
54+
@app.blob_input(arg_name="inputblob",
55+
path="sample-workitems/{name}",
56+
connection="AzureWebJobsStorage")
57+
def test(req: func.HttpRequest, inputblob: bytes) -> func.HttpResponse:
58+
logging.info(f'Python Queue trigger function processed {len(inputblob)} bytes')
59+
return inputblob
60+
```
61+
62+
## Azure Blob storage output binding
63+
64+
```python
65+
import logging
66+
import azure.functions as func
67+
app = func.FunctionApp()
68+
@app.function_name(name="BlobOutput1")
69+
@app.route(route="file")
70+
@app.blob_input(arg_name="inputblob",
71+
path="sample-workitems/test.txt",
72+
connection="AzureWebJobsStorage")
73+
@app.blob_output(arg_name="outputblob",
74+
path="newblob/test.txt",
75+
connection="AzureWebJobsStorage")
76+
def main(req: func.HttpRequest, inputblob: str, outputblob: func.Out[str]):
77+
logging.info(f'Python Queue trigger function processed {len(inputblob)} bytes')
78+
outputblob.set(inputblob)
79+
return "ok"
80+
```
81+
4682
## Azure Cosmos DB trigger
4783

4884
The following code snippet defines a function triggered from an Azure Cosmos DB (SQL API):
@@ -52,13 +88,45 @@ import logging
5288
import azure.functions as func
5389
app = func.FunctionApp()
5490
@app.function_name(name="CosmosDBTrigger1")
55-
@app.cosmos_db_trigger(arg_name="documents", database_name="<DB_NAME>", collection_name="<COLLECTION_NAME>", connection_string_setting="<COSMOS_CONNECTION_SETTING>",
91+
@app.cosmos_db_trigger(arg_name="documents", database_name="<DB_NAME>", collection_name="<COLLECTION_NAME>", connection_string_setting=""AzureWebJobsStorage"",
5692
lease_collection_name="leases", create_lease_collection_if_not_exists="true")
5793
def test_function(documents: func.DocumentList) -> str:
5894
if documents:
5995
logging.info('Document id: %s', documents[0]['id'])
6096
```
6197

98+
## Azure Cosmos DB input binding
99+
100+
```python
101+
import logging
102+
import azure.functions as func
103+
app = func.FunctionApp()
104+
@app.route()
105+
@app.cosmos_db_input(
106+
arg_name="documents", database_name="<DB_NAME>",
107+
collection_name="<COLLECTION_NAME>",
108+
connection_string_setting="AzureWebJobsStorage")
109+
def cosmosdb_input(req: func.HttpRequest, documents: func.DocumentList) -> str:
110+
return func.HttpResponse(documents[0].to_json())
111+
```
112+
113+
## Azure Cosmos DB output binding
114+
115+
```python
116+
import logging
117+
import azure.functions as func
118+
@app.route()
119+
@app.cosmos_db_output(
120+
arg_name="documents", database_name="<DB_NAME>",
121+
collection_name="<COLLECTION_NAME>",
122+
create_if_not_exists=True,
123+
connection_string_setting="AzureWebJobsStorage")
124+
def main(req: func.HttpRequest, documents: func.Out[func.Document]) -> func.HttpResponse:
125+
request_body = req.get_body()
126+
documents.set(func.Document.from_json(request_body))
127+
return 'OK'
128+
```
129+
62130
## Azure EventHub trigger
63131

64132
The following code snippet defines a function triggered from an event hub instance:
@@ -69,12 +137,32 @@ import azure.functions as func
69137
app = func.FunctionApp()
70138
@app.function_name(name="EventHubTrigger1")
71139
@app.event_hub_message_trigger(arg_name="myhub", event_hub_name="samples-workitems",
72-
connection="<EVENT_HUB_CONNECTION_SETTING>")
140+
connection=""AzureWebJobsStorage"")
73141
def test_function(myhub: func.EventHubEvent):
74142
logging.info('Python EventHub trigger processed an event: %s',
75143
myhub.get_body().decode('utf-8'))
76144
```
77145

146+
## Azure EventHub output binding
147+
148+
```python
149+
import logging
150+
import azure.functions as func
151+
app = func.FunctionApp()
152+
@app.function_name(name="eventhub_output")
153+
@app.route(route="eventhub_output")
154+
@app.event_hub_output(arg_name="event",
155+
event_hub_name="samples-workitems",
156+
connection="AzureWebJobsStorage")
157+
def eventhub_output(req: func.HttpRequest, event: func.Out[str]):
158+
body = req.get_body()
159+
if body is not None:
160+
event.set(body.decode('utf-8'))
161+
else:
162+
logging.info('req body is none')
163+
return 'ok'
164+
```
165+
78166
## HTTP trigger
79167

80168
The following code snippet defines an HTTP triggered function:
@@ -104,28 +192,45 @@ def test_function(req: func.HttpRequest) -> func.HttpResponse:
104192
)
105193
```
106194

107-
## Azure Queue Storage trigger
195+
## Azure Queue storage trigger
108196

109197
```python
110198
import logging
111199
import azure.functions as func
112200
app = func.FunctionApp()
113201
@app.function_name(name="QueueTrigger1")
114202
@app.queue_trigger(arg_name="msg", queue_name="python-queue-items",
115-
connection="")
203+
connection=""AzureWebJobsStorage"")
116204
def test_function(msg: func.QueueMessage):
117205
logging.info('Python EventHub trigger processed an event: %s',
118206
msg.get_body().decode('utf-8'))
119207
```
120208

209+
## Azure Queue storage output binding
210+
211+
```python
212+
import logging
213+
import azure.functions as func
214+
app = func.FunctionApp()
215+
@app.function_name(name="QueueOutput1")
216+
@app.route(route="message")
217+
@app.queue_output(arg_name="msg", queue_name="python-queue-items", connection="AzureWebJobsStorage")
218+
def main(req: func.HttpRequest, msg: func.Out[str]) -> func.HttpResponse:
219+
input_msg = req.params.get('name')
220+
msg.set(input_msg)
221+
logging.info(input_msg)
222+
logging.info('name: {name}')
223+
return 'OK'
224+
```
225+
121226
## Azure Service Bus queue trigger
122227

123228
```python
124229
import logging
125230
import azure.functions as func
126231
app = func.FunctionApp()
127232
@app.function_name(name="ServiceBusQueueTrigger1")
128-
@app.service_bus_queue_trigger(arg_name="msg", queue_name="myinputqueue", connection="")
233+
@app.service_bus_queue_trigger(arg_name="msg", queue_name="myinputqueue", connection="AzureWebJobsStorage")
129234
def test_function(msg: func.ServiceBusMessage):
130235
logging.info('Python ServiceBus queue trigger processed message: %s',
131236
msg.get_body().decode('utf-8'))
@@ -138,13 +243,30 @@ import logging
138243
import azure.functions as func
139244
app = func.FunctionApp()
140245
@app.function_name(name="ServiceBusTopicTrigger1")
141-
@app.service_bus_topic_trigger(arg_name="message", topic_name="mytopic", connection="", subscription_name="testsub")
246+
@app.service_bus_topic_trigger(arg_name="message", topic_name="mytopic", connection="AzureWebJobsStorage", subscription_name="testsub")
142247
def test_function(message: func.ServiceBusMessage):
143248
message_body = message.get_body().decode("utf-8")
144249
logging.info("Python ServiceBus topic trigger processed message.")
145250
logging.info("Message Body: " + message_body)
146251
```
147252

253+
## Azure Service Bus Topic output binding
254+
255+
```python
256+
import logging
257+
import azure.functions as func
258+
app = func.FunctionApp()
259+
@app.route(route="put_message")
260+
@app.service_bus_topic_output(
261+
arg_name="message",
262+
connection="AzureWebJobsStorage",
263+
topic_name="mytopic")
264+
def main(req: func.HttpRequest, message: func.Out[str]) -> func.HttpResponse:
265+
input_msg = req.params.get('message')
266+
message.set(input_msg)
267+
return 'OK'
268+
```
269+
148270
## Timer trigger
149271

150272
```python

articles/azure-functions/functions-reference-python.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ At this time, only specific triggers and bindings are supported by the Python v2
412412
| [Azure Service Bus topic](functions-bindings-triggers-python.md#azure-service-bus-topic-trigger) | x | | x |
413413
| [Azure Service Bus queue](functions-bindings-triggers-python.md#azure-service-bus-queue-trigger) | x | | x |
414414
| [Azure Cosmos DB](functions-bindings-triggers-python.md#azure-eventhub-trigger) | x | x | x |
415-
| [Azure Blob Storage](functions-bindings-triggers-python.md#blob-trigger) | x | x | x |
415+
| [Azure Blob Storage](functions-bindings-triggers-python.md#azure-blob-storage-trigger) | x | x | x |
416416
| [Azure Hub](functions-bindings-triggers-python.md#azure-eventhub-trigger) | x | | x |
417417

418418
For more examples, see [Python V2 model Azure Functions triggers and bindings (preview)](functions-bindings-triggers-python.md).

0 commit comments

Comments
 (0)