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

标签 python azure azure-functions

我在 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/

相关文章:

python - 更快的 SciPy 优化

azure - 如何在 Azure Devops 上安装多个 .NET Core SDK 版本

Azure 容器注册表比从 hub.docker.com 获取镜像慢

azure - 是否可以在不使用 azure 门户的情况下查看 azure 函数执行日志?

c# - Azure Functions 中的 ReportViewer : mscorlib: The path is not of a legal form

python - 在 sqlite3 中插入​​大量记录(超过 40,000 条)的优化方法是什么

python - 如何将 travis ci 与 Python 中的 codeclimate 测试覆盖率集成?

Azure 资源图查询查找具有公共(public) IP 的所有 VM

azure - 哪些 Azure 服务可以帮助我摆脱 VM?

python - 是否可以在Python中运行实例变量