我在 Azure 存储中设置 blob 元数据时遇到问题。我在 Spyder(即本地 Python)中为此开发了一个脚本,效果很好。现在,我希望能够作为 Azure 函数执行相同的脚本。但是,在设置元数据时,出现以下错误:HttpResponseError:指定的资源名称包含无效字符。
我从 Spyder 到 Functions 所做的唯一更改是:
spy :
def main(container_name,blob_name,metadata):
from azure.storage.blob import BlobServiceClient
# Connection string to storage account
storageconnectionstring=secretstoragestringnotforstackoverflow
# initialize clients
blobclient_from_connectionstring=BlobServiceClient.from_connection_string(storageconnectionstring)
containerclient=blobclient_from_connectionstring.get_container_client(container_name)
blob_client = containerclient.get_blob_client(blob_name)
# set metadata of container
blob_client.set_blob_metadata(metadata=metadata)
return
函数
def main(req: func.HttpRequest):
container_name = req.params.get('container_name')
blob_name = req.params.get('blob_name')
metadata_raw = req.params.get('metadata')
metadata_json = json.loads(metadata_raw)
# Connection string to storage account
storageconnectionstring=secretstoragestringnotforstackoverflow
# initialize clients
blobclient_from_connectionstring=BlobServiceClient.from_connection_string(storageconnectionstring)
containerclient=blobclient_from_connectionstring.get_container_client(container_name)
blob_client = containerclient.get_blob_client(blob_name)
# set metadata of container
blob_client.set_blob_metadata(metadata=metadata_json)
return func.HttpResponse()
函数的参数在 header 中传递。问题在于元数据,而不是 container_name 或 blob_name,因为当我注释掉元数据时,我没有收到错误。另外,我尝试使用单引号或双引号以及 JSON 或字符串以多种形式格式化元数据,但到目前为止还没有成功。谁能帮我解决这个问题?
最佳答案
我能够解决这个问题。脚本没问题,问题出在输入参数上。它们需要采用特定的格式。元数据作为带双引号的字典,blob/容器作为不带任何引号的字符串。
作为请求的function.json:
{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}
带参数格式: Picture from Azure Functions
关于python - 使用 Python SDK 和 Azure Functions 将元数据添加到 Azure blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64913504/