如何使用python在Azure函数中设置名称和位置的输出绑定?

时间:2019-07-17 20:08:57

标签: python-3.x azure-functions azure-storage

我似乎不太了解如何使文件保存到Blob存储的输出绑定。我使用Python创建了一个Azure函数,该函数使用CosmosDB更改Feed触发器。我需要将该文档保存到Blob存储。 我已经按如下所示设置了function.json文件:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "cosmosDBTrigger",
      "name": "documents",
      "direction": "in",
      "leaseCollectionName": "leases",
      "connectionStringSetting": "cosmos_dev",
      "databaseName": "MyDatabase",
      "collectionName": "MyCollection",
      "createLeaseCollectionIfNotExists": "true"
    },
    {
      "type": "blob",
      "direction": "out",
      "name": "outputBlob",
      "path": "raw/changefeedOutput/{blobname}",
      "connection": "blobStorageConnection"
    }
  ]
}

因此触发器将获得如下文档:

{ "id": "documentId-12345",
  other sections here
  "entity": "customer"
}

init .py文件中,我的基本代码为

def main(documents: func.DocumentList) -> func.Document:
        logging.info(f"CosmosDB trigger executed!")
        for doc in documents:
                blobName = doc['id'] + '.json'
                blobFolder= doc['entity'] 
                blobData = doc.to_json()

我认为我需要在def中添加诸如'outputBlob:func.Out'之类的内容,但不确定如何进行

查看github上的示例

https://github.com/yokawasa/azure-functions-python-samples/tree/master/v2functions/blob-trigger-watermark-blob-out-binding

看来我必须 outputBlob.set(某物) 因此,我正在寻找如何设置def部件并将blob从cosmosdb文档中的数据发送到我设置的位置的方法。

我尝试了以下方法:

def main(documents: func.DocumentList, outputBlob: func.Out[str] ) -> func.Document:
        logging.info(f"CosmosDB trigger executed!")
        for doc in documents:
                blobName = doc['id'] + '.json'
                outputBlob.set(blobName)

并得到结果:

CosmosDB trigger executed!
Executed 'Functions.CosmosTrigger_py' (Failed, Id=XXXXX)
System.Private.CoreLib: Exception while executing function: Functions.CosmosTrigger_py. Microsoft.Azure.WebJobs.Host: No value for named parameter 'blobname'.

我可以从os.enviro中调用连接内容,然后以这种方式获取连接字符串,我认为并使用带有位置,名称和Blob数据的标准create_blob_from_text,

block_blob_service.create_blob_from_text(blobLocation,blobName,formattedBlob)

任何指针都很棒

0 个答案:

没有答案