使用Python SDK和Azure函数向Azure blob添加元数据

2024-05-29 08:17:09 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

函数的参数在头中传递。问题在于元数据,而不是容器名称或blob\u名称,因为我在注释元数据时没有得到错误。此外,我还尝试用单引号或双引号将元数据格式化为JSON或字符串,但到目前为止运气不佳。谁能帮我解决这个问题


Tags: 数据namefromclientgetstringcontainerreq
1条回答
网友
1楼 · 发布于 2024-05-29 08:17:09

我能解决这个问题。脚本很好,问题在于输入参数。它们需要采用特定的格式。元数据作为带有双引号的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

相关问题 更多 >

    热门问题