【问题标题】:Adding metadata to Azure blob with Python SDK & Azure Functions使用 Python SDK 和 Azure Functions 将元数据添加到 Azure Blob
【发布时间】:2020-11-23 22:51:35
【问题描述】:

在 Azure 存储中设置 blob 元数据时遇到问题。我在 Spyder 中为此开发了一个脚本,所以本地 Python 非常好用。现在,我希望能够像 Azure 函数一样执行相同的脚本。但是,在设置元数据时出现以下错误:HttpResponseError: The specifed resource name contains invalid characters.

我所做的从 Spyder 到 Functions 的唯一更改是:

间谍:

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 或字符串格式化许多变体中的元数据,但到目前为止没有运气。谁能帮我解决这个问题?

【问题讨论】:

    标签: python azure azure-functions


    【解决方案1】:

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

    作为请求的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

    【讨论】:

    • 嘿,@Enzo L. 您能否发布您的 function.json 文件内容并接受您的答案以供其他人参考。
    • 确定,我加了
    猜你喜欢
    • 1970-01-01
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 2021-06-26
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多