@@ -26,7 +26,7 @@ To create your first function in the new v2 model, see one of these quickstart a
26
26
+ [ Get started with Visual Studio] ( ./create-first-function-vs-code-python.md )
27
27
+ [ Get started command prompt] ( ./create-first-function-cli-python.md )
28
28
29
- ## Blob trigger
29
+ ## Azure Blob storage trigger
30
30
31
31
The following code snippet defines a function triggered from Azure Blob Storage:
32
32
@@ -36,13 +36,49 @@ import azure.functions as func
36
36
app = func.FunctionApp()
37
37
@app.function_name (name = " BlobTrigger1" )
38
38
@app.blob_trigger (arg_name = " myblob" , path = " samples-workitems/{name} " ,
39
- connection = " <STORAGE_CONNECTION_SETTING> " )
39
+ connection = " AzureWebJobsStorage " )
40
40
def test_function (myblob : func.InputStream):
41
41
logging.info(f " Python blob trigger function processed blob \n "
42
42
f " Name: { myblob.name} \n "
43
43
f " Blob Size: { myblob.length} bytes " )
44
44
```
45
45
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
+
46
82
## Azure Cosmos DB trigger
47
83
48
84
The following code snippet defines a function triggered from an Azure Cosmos DB (SQL API):
@@ -52,13 +88,45 @@ import logging
52
88
import azure.functions as func
53
89
app = func.FunctionApp()
54
90
@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 " " ,
56
92
lease_collection_name = " leases" , create_lease_collection_if_not_exists = " true" )
57
93
def test_function (documents : func.DocumentList) -> str :
58
94
if documents:
59
95
logging.info(' Document id: %s ' , documents[0 ][' id' ])
60
96
```
61
97
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
+
62
130
## Azure EventHub trigger
63
131
64
132
The following code snippet defines a function triggered from an event hub instance:
@@ -69,12 +137,32 @@ import azure.functions as func
69
137
app = func.FunctionApp()
70
138
@app.function_name (name = " EventHubTrigger1" )
71
139
@app.event_hub_message_trigger (arg_name = " myhub" , event_hub_name = " samples-workitems" ,
72
- connection = " <EVENT_HUB_CONNECTION_SETTING> " )
140
+ connection = " " AzureWebJobsStorage " " )
73
141
def test_function (myhub : func.EventHubEvent):
74
142
logging.info(' Python EventHub trigger processed an event: %s ' ,
75
143
myhub.get_body().decode(' utf-8' ))
76
144
```
77
145
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
+
78
166
## HTTP trigger
79
167
80
168
The following code snippet defines an HTTP triggered function:
@@ -104,28 +192,45 @@ def test_function(req: func.HttpRequest) -> func.HttpResponse:
104
192
)
105
193
```
106
194
107
- ## Azure Queue Storage trigger
195
+ ## Azure Queue storage trigger
108
196
109
197
``` python
110
198
import logging
111
199
import azure.functions as func
112
200
app = func.FunctionApp()
113
201
@app.function_name (name = " QueueTrigger1" )
114
202
@app.queue_trigger (arg_name = " msg" , queue_name = " python-queue-items" ,
115
- connection = " " )
203
+ connection = " " AzureWebJobsStorage " " )
116
204
def test_function (msg : func.QueueMessage):
117
205
logging.info(' Python EventHub trigger processed an event: %s ' ,
118
206
msg.get_body().decode(' utf-8' ))
119
207
```
120
208
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
+
121
226
## Azure Service Bus queue trigger
122
227
123
228
``` python
124
229
import logging
125
230
import azure.functions as func
126
231
app = func.FunctionApp()
127
232
@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 " )
129
234
def test_function (msg : func.ServiceBusMessage):
130
235
logging.info(' Python ServiceBus queue trigger processed message: %s ' ,
131
236
msg.get_body().decode(' utf-8' ))
@@ -138,13 +243,30 @@ import logging
138
243
import azure.functions as func
139
244
app = func.FunctionApp()
140
245
@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" )
142
247
def test_function (message : func.ServiceBusMessage):
143
248
message_body = message.get_body().decode(" utf-8" )
144
249
logging.info(" Python ServiceBus topic trigger processed message." )
145
250
logging.info(" Message Body: " + message_body)
146
251
```
147
252
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
+
148
270
## Timer trigger
149
271
150
272
``` python
0 commit comments