python - Blob input binding with Azure Function in Python is not working (inputblob.name is None"). How to troubleshoot?
问题描述
I have the following simple Azure Function written in Python. It is an HTTP trigger that should simply return the name and URI of the blob input binding from an Azure storage account (reference documentation from Microsoft here).
import logging
import azure.functions as func
import azure.storage.blob
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
def main(req: func.HttpRequest, inputblob: func.InputStream) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
return func.HttpResponse(f"Blob name: {inputblob.name}. Blob URI: {inputblob.uri}")
My function.json file looks like this. I've verified that the connection string in local.settings.json is correct and that the blob path is correct too.
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"name": "inputblob",
"type": "blob",
"path": "containername/testblobname.json",
"connection": "MyStorageConnectionAppSetting",
"direction": "in"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
Now, this function returns "JSON name: None. JSON URI: None". Clearly the blob input binding is not working. How do I troubleshoot this, or what am I missing?
解决方案
According to my research, at the moment, the metadata of the blob input binding (e.g. name, length) is not provided by the function host, but you can still access the raw data in blob binding via blob.read()
. For more details, please refer to https://github.com/Azure/azure-functions-python-worker/issues/576.
My function.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"name": "inputblob",
"type": "blob",
"path": "input/keyCredentials.txt",
"connection": "MyStorageConnectionAppSetting",
"direction": "in"
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
My code
import logging
import azure.functions as func
def main(req: func.HttpRequest, inputblob: func.InputStream) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
return func.HttpResponse(f"Blob conetnt: {inputblob.read()}.")
推荐阅读
- python - flask-sqlalchemy 外连接的 Flask-marshmallow 转储返回空
- php - 如何使用 PHP foreach 正确设置数组的值?
- mongodb - Spring Boot - 避免多次获取相同的 DAO
- c++ - C ++有没有办法让getline读取由空格分隔的一行中的2个数字?
- python - 如何在views.py中制作频道客户端?
- c# - 我如何获得高分?
- botframework - 是否可以在来自用户的消息中包含换行符 (\n) 以处理 Teams 中的自适应卡?
- python - 如何优化两个矩阵的所有向量之间的计算距离?
- java - Apache POI SXSSFWorkbook 单独自动运行时无法创建工作表
- python - Python - Run script periodically