【发布时间】:2020-04-03 06:24:24
【问题描述】:
我想使用 Python 中的 Azure Functions 将 JSON 数据作为 .json 文件上传到 Azure 存储 Blob。
因为我使用的是 Azure Functions 而不是实际的服务器,所以我不想(也可能不能)在本地内存中创建一个临时文件并使用 Azure Blob 存储客户端库 v2.1 将该文件上传到 Azure Blob 存储对于 Python (reference link here)。因此,我想为 Azure Functions (reference link here) 使用输出 blob 存储绑定。
我正在使用 HTTP 触发器在 Azure Functions 中对此进行测试。我通过输入 blob 存储绑定(工作正常)接收 Azure blob,处理它,并通过上传覆盖它的新 Azure blob 来更新它(这是我需要帮助的)。我的 function.json 文件如下所示:
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"name": "inputblob",
"type": "blob",
"path": "{containerName}/{blobName}.json",
"connection": "MyStorageConnectionAppSetting",
"direction": "in"
},
{
"name": "outputblob",
"type": "blob",
"path": "{containerName}/{blobName}.json",
"connection": "MyStorageConnectionAppSetting",
"direction": "out"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
我的 Python 代码如下所示:
import logging
import azure.functions as func
import azure.storage.blob
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import json, os
def main(req: func.HttpRequest, inputblob: func.InputStream, outputblob: func.Out[func.InputStream]) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
# Initialize variable for tracking any changes
anyChanges= False
# Read JSON file
jsonData= json.loads(inputblob.read())
# Make changes to jsonData (omitted for simplicity) and update anyChanges
# Upload new JSON file
if anyChanges:
outputblob.set(jsonData)
return func.HttpResponse(f"Input data: {jsonData}. Any changes: {anyChanges}.")
但是,这根本不起作用,引发以下错误 (screenshot):
值 'func.Out' 不可下标
我错过了什么?
【问题讨论】:
标签: python azure azure-functions azure-blob-storage