Skip to content

Commit 9d252d1

Browse files
author
Yoichi Kawasaki
committed
Updated blob-trigger-cosmosdb-out-binding
1 parent 4ad3cf1 commit 9d252d1

File tree

2 files changed

+91
-3
lines changed

2 files changed

+91
-3
lines changed

v2functions/blob-trigger-cosmosdb-out-binding/__init__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import requests
66

77
subscription_key = os.environ['ComputerVisionSubscription']
8+
api_endpoint = os.environ['ComputerVisionApiEndpoint']
89

910
headers = {
1011
# Request headers.
@@ -25,8 +26,9 @@ def main(myblob: func.InputStream, doc: func.Out[func.Document]):
2526

2627
img_data = myblob.read()
2728
try:
28-
api_url = "https://westus.api.cognitive.microsoft.com/vision/v1.0/analyze?%s" % params
29-
print ("API URL:{}".format(api_url))
29+
api_url = "{0}vision/v1.0/analyze?{1}".format(api_endpoint, params)
30+
logging.info("API URL:{}".format(api_url))
31+
3032
r = requests.post(api_url,
3133
headers=headers,
3234
data=img_data)

v2functions/blob-trigger-cosmosdb-out-binding/readme.md

+87-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@
44
| ------------- | ------------- | ------------- | ----------- | ----------- |
55
| `cosmosdb-trigger-cosmosdb-in-binding` | Azure Functions Blob Storage Trigger Python Sample. The function gets image data from Azure Blob Trigger, gets tags for the image with [Computer Vision API](https://azure.microsoft.com/en-us/services/cognitive-services/computer-vision/) ([Azure Cognitive Services](https://azure.microsoft.com/en-us/services/cognitive-services/)), and store the tags into Azure Cosmos DB by leveraging CosmosDB output binding | Blob Storage | NONE | CosmosDB |
66

7+
<!-- TOC -->
8+
- [blob-trigger-cosmosdb-out-binding (Python)](#blob-trigger-cosmosdb-out-binding-python)
9+
- [Configurations](#configurations)
10+
- [Create Computer Vision resource](#create-computer-vision-resource)
11+
- [Create Blob Storage account & Container](#create-blob-storage-account--container)
12+
- [Create Cosmos DB Account and DB & Collection](#create-cosmos-db-account-and-db--collection)
13+
- [How to develop and publish the function](#how-to-develop-and-publish-the-function)
14+
- [Local development](#local-development)
15+
- [Publish the function to the cloud](#publish-the-function-to-the-cloud)
16+
- [Test Request](#test-request)
17+
- [HTTP Request body format](#http-request-body-format)
18+
- [Response body format](#response-body-format)
19+
720
## Configurations
821
As specified in `functions.json`, you need Azure Storage account for triggering functions and Cosmos DB Account to store data using Cosmos DB output binding
922

@@ -31,12 +44,42 @@ As specified in `functions.json`, you need Azure Storage account for triggering
3144
}
3245
]
3346
}
47+
```
48+
49+
### Create Computer Vision resource
50+
51+
First, create a Computer Vision resource
52+
53+
```bash
54+
COGNITIVE_RESOURCE_GROUP="rg_cognitive_test"
55+
REGION="eastasia"
56+
COGNITIVE_ACCOUNT_NAME="mycompvision001"
57+
58+
echo "Create Resource Group: $COGNITIVE_RESOURCE_GROUP"
59+
az group create --name $COGNITIVE_RESOURCE_GROUP --location $REGION
60+
61+
echo "Create Cognitive Resource for Computer Vision: $COGNITIVE_ACCOUNT_NAME"
62+
az cognitiveservices account create \
63+
-n $COGNITIVE_ACCOUNT_NAME \
64+
-g $COGNITIVE_RESOURCE_GROUP \
65+
--kind ComputerVision \
66+
--sku S1 \
67+
-l $REGION \
68+
--yes
69+
```
3470

71+
Then, Get Computer Vision API Key and endpoint. You'll use the values in later step:
72+
```bash
73+
COMPUTER_VISION_API_ENDPOINT=$(az cognitiveservices account show -n $COGNITIVE_ACCOUNT_NAME -g $COGNITIVE_RESOURCE_GROUP --output tsv |awk '{print $1}')
74+
COMPUTER_VISION_API_KEY=$(az cognitiveservices account keys list -n $COGNITIVE_ACCOUNT_NAME -g $COGNITIVE_RESOURCE_GROUP --output tsv |awk '{print $1}')
75+
echo "API Endpoint: $COMPUTER_VISION_API_ENDPOINT"
76+
echo "API KEY: $COMPUTER_VISION_API_KEY"
3577
```
78+
3679
### Create Blob Storage account & Container
3780

3881
Create an Azure Storage Account
39-
```sh
82+
```bash
4083
RESOURCE_GROUP="rg-testfunctions"
4184
REGION="japaneast"
4285
STORAGE_ACCOUNT="teststore"
@@ -127,6 +170,49 @@ az webapp config appsettings set \
127170
-n $FUNCTION_APP_NAME \
128171
-g $RESOURCE_GROUP \
129172
--settings \
173+
ComputerVisionSubscription=$COMPUTER_VISION_API_KEY \
174+
ComputerVisionApiEndpoint=$COMPUTER_VISION_API_ENDPOINT \
130175
MyStorageConnectionString=$FUNCTION_STORAGE_CONNECTION \
131176
MyCosmosDBConnectionString=$COSMOS_DB_CONNECTION
132177
```
178+
179+
## Test Request
180+
181+
### HTTP Request body format
182+
HTTP Request body must include the following parameters:
183+
```
184+
{
185+
'permission': '<Signed permission for shared access signature (Required)>',
186+
'container': '<Container name to access (Required)>',
187+
'blobname': '<Blob object name to access (Optional)>'
188+
'ttl': '<Token time to live period in hours. 1hour by default (Optional)>'
189+
}
190+
```
191+
192+
The following values can be used for permissions:
193+
`a` (Add), `r` (Read), `w` (Write), `d` (Delete), `l` (List)
194+
Concatenate multiple permissions, such as `rwa` = Read, Write, Add
195+
196+
Sample Request Body
197+
```
198+
{
199+
'permission': "rl",
200+
'container': "functiontest",
201+
'blobname': "sample.png"
202+
'ttl': 2
203+
}
204+
```
205+
206+
## Response body format
207+
HTTP response body format is:
208+
```
209+
{
210+
'token': '<Shared Access Signature Token string>',
211+
'url' : '<SAS resource URI>'
212+
}
213+
```
214+
215+
Sample Response Body
216+
```
217+
{'url': 'https://testfunction.blob.core.windows.net/functiontest/yokawasa.png?sig=sXBjML1Fpk9UnTBtajo05ZTFSk0LWFGvARZ6WlVcAog%3D&srt=o&ss=b&spr=https&sp=rl&sv=2016-05-31&se=2017-07-01T00%3A21%3A38Z&st=2017-07-01T23%3A16%3A38Z', 'token': 'sig=sXBjML1Fpk9UnTBtajo05ZTFSk0LWFGvARZ6WlVcAog%3D&srt=o&ss=b&spr=https&sp=rl&sv=2016-05-31&se=2017-07-01T00%3A21%3A38Z&st=2017-07-01T23%3A16%3A38Z'}
218+
```

0 commit comments

Comments
 (0)