【问题标题】:Can't read Content/Data with BlobTrigger (azure.functions) [Python]无法使用 BlobTrigger (azure.functions) [Python] 读取内容/数据
【发布时间】:2021-12-14 08:15:23
【问题描述】:

我在使用 Python 和 BlobTrigger 读取内容/数据时遇到问题。 我使用本地环境并遵循文档 (https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-trigger?tabs=python)。 当我将文件上传到本地 blob 模拟器时,该函数正在侦听并且成功触发。 我也可以在变量中获取文件路径/文件名,但无法读取上传文件的内容。

当我尝试获取内容时,它总是显示一个空字符串或数组。

这是我的functions.json文件:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myblob",
      "type": "blobTrigger",
      "direction": "in",
      "path": "log/SystemLog/{name}",
      "connection": ""
    }
  ]
}

这是我的 init.py 文件: 导入日志 将 azure.functions 导入为 func

def main(myblob: func.InputStream):    
    
    print(myblob.name)
    print(myblob.length)
    print(myblob.readlines())

这是在控制台中打印的内容:

[2021-10-29T07:27:25.053Z] Host lock lease acquired by instance ID '000000000000000000000000F86KCB51'.
[2021-10-29T07:27:25.124Z] Worker process started and initialized.
[2021-10-29T07:27:46.941Z] Executing 'Functions.BlobTriggerLocalTest' (Reason='New blob detected: log/SystemLog/testfile.txt', Id=3981bd58-accb-4c9c-b3e4-fe33b1a74522)
[2021-10-29T07:27:46.948Z] Trigger Details: MessageId: 7c575bad-88b7-46d4-b5bf-67b90fe0ab4d, DequeueCount: 1, InsertionTime: 2021-10-29T07:27:46.000+00:00, BlobCreated: 2021-10-29T07:27:43.000+00:00, BlobLastModified: 2021-10-29T07:27:43.000+00:00
[2021-10-29T07:27:47.032Z] log/SystemLog/testfile.txt
[2021-10-29T07:27:47.037Z] None
[2021-10-29T07:27:47.042Z] []
[2021-10-29T07:27:47.068Z] Executed 'Functions.BlobTriggerLocalTest' (Succeeded, Id=3981bd58-accb-4c9c-b3e4-fe33b1a74522, Duration=204ms)

我尝试了一些解决方案来解决这个问题。

第一件事是配合文档https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob-input?tabs=python。 但是对我来说,functions.json 文件中的变化并不完全清楚。我试着输入

{
      "name": "inputblob",
      "type": "blob",
      "dataType": "binary",
      "path": "log/SystemLog/{name}",
      "connection": "",
      "direction": "in"
    },

在 functions.json 文件中,并将 inputblob 作为第二个参数添加到主方法中,但 len(inputblob) 也会打印 0。

我想我也可以使用并从 azure.storage.blob 导入 BlobServiceClient。 但我猜 BlobServiceClient 需要一个单独的连接字符串,我想避免这种情况。

【问题讨论】:

    标签: python azure azure-functions


    【解决方案1】:

    解决方法很简单: 不要使用 Visual Studio Code 作为文件资源管理器来通过拖放进行上传。

    本地环境的文档引导我使用 VS Code 作为本地 blob 的文件资源管理器。将文件从 Desktop 上传到 blob 会导致文件内容丢失。该文件以正确的文件名上传,但内部为空。 使用 MS Storage Explorer 解决了这个问题。

    【讨论】:

      【解决方案2】:

      我们可以通过使用download_blob()函数并在其中添加content_as_text来读取python代码中的内容。

      下面是示例代码:

      import logging
      import sys
      import os
      import azure.functions as func
      from azure.storage import blob
      from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, __version__
      
      def main(myblob: func.InputStream):
          try:
              logging.info(f"Python blob trigger function processed blob \n")
              CONN_STR = "ADD_CON_STR"
              blob_service_client = BlobServiceClient.from_connection_string(CONN_STR)
      
              # MAP SOURCE FILE
              blob_client = blob_service_client.get_blob_client(container="newcontainer0805", blob="source.txt")
      
              #SOURCE CONTENTS
              content =  blob_client.download_blob().content_as_text
              print(content)
      

      稍后您可以将内容打印为 print(content)。

      【讨论】:

      • 当然,但我希望输入参数“myblob”包含 blob。我想避免使用新连接加载它。
      猜你喜欢
      • 2022-06-11
      • 1970-01-01
      • 2019-08-12
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多