使用 Python SDK 和 Azure Functions 将元数据添加到 Azure blob



在Azure存储中设置blob元数据时遇到问题。我已经在Spyder中为此开发了一个脚本,所以使用本地Python,效果很好。现在,我希望能够执行与Azure函数相同的脚本。然而,当设置元数据时,我会得到以下错误:HttpResponseError: The specifed resource name contains invalid characters.

从Spyder到我所做的功能的唯一改变是:

Spyder:

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()

函数的参数在标头中传递。问题在于元数据,而不是container_name或blob_name,因为当我注释掉元数据时没有错误。此外,我还尝试过用单引号或双引号以及JSON或字符串格式化多种变体的元数据,但到目前为止没有成功。有谁能帮我解决这个问题吗?

我解决了这个问题。脚本很好,输入参数有问题。它们需要采用特定的格式。元数据作为带双引号的dict,blob/container作为不带任何引号的字符串。

应要求提供函数.json:

{
"scriptFile": "__init__.py",
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "$return"
}
]
}

使用参数格式:图片来自Azure功能

最新更新