简体   繁体   English

如何使用 Azure 函数从 Blob 存储中读取 json 文件 Python

[英]How to read json file from blob storage using Azure Functions Blob Trigger with Python

I have created a BlobTrigger in Azure Function App to read any new file getting inserted or updated in my Azure Blob storage.我在 Azure Function 应用程序中创建了一个 BlobTrigger,以读取在我的 Azure Blob 存储中插入或更新的任何新文件。

Trigger is working properly to identify latest files inserted or updated in my blob container as well as I am able to print the json body of the file.触发器正常工作以识别在我的 blob 容器中插入或更新的最新文件,并且我能够打印文件的 json 正文。

However when I am trying to store the json object in a variable to transform it, it throws an error.但是,当我尝试将 json object 存储在变量中以对其进行转换时,它会引发错误。

I would like assign each key of the json to a variable.我想将 json 的每个键分配给一个变量。 My json is我的 json 是

   {
    "name":"Saikat",
    "id":"1234"
   }

Below is code when I can print the json and error while trying to store it.下面是当我可以打印 json 和尝试存储它时出错的代码。

   import logging
   import azure.functions as func
   import json

   def main(myblob: func.InputStream):
       logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")
        print("JSON Body",json.load(myblob))

在此处输入图像描述

   import logging
   import azure.functions as func
   import json

   def main(myblob: func.InputStream):
       logging.info(f"Python blob trigger function processed blob \n"
                 f"Name: {myblob.name}\n"
                 f"Blob Size: {myblob.length} bytes")
        print("JSON Body",json.load(myblob))
        #Store JSON file
        jsonData= json.load(myblob)
        print("****jsonData*****",jsonData)

在此处输入图像描述

Essentially you're getting this error is because you're reading from the stream twice.本质上,您收到此错误是因为您从 stream 读取了两次。 After the 1st read, the stream's read position is set at the end of the stream and that's why your 2nd read is failing.在第一次读取之后,流的读取 position 设置在 stream 的末尾,这就是第二次读取失败的原因。

Based on the comments below, since InputStream BytesIO object doesn't contain seek operation, the solution to your problem is to read the stream just once.根据下面的评论,由于 InputStream BytesIO object 不包含查找操作,因此您的问题的解决方案是仅读取 stream 一次。

Try something like the following:尝试以下操作:

import logging
import azure.functions as func
import json

def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob \n"
                f"Name: {myblob.name}\n"
                f"Blob Size: {myblob.length} bytes")
    jsonData= json.load(myblob)
    print("JSON Body",jsonData)

    #Store JSON file
    print("****jsonData*****",jsonData)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 python azure 函数从 azure blob 存储读取文件 - Read files from azure blob storage using python azure functions 如何在 Python 中使用 Azure Functions 的 Azure Blob 存储绑定将 JSON 数据上传到 Azure 存储 blob - How to upload JSON data to Azure storage blob using Azure Blob storage bindings for Azure Functions in Python 从 python 中的 azure blob 存储读取文件 - read file from azure blob storage in python 使用python azure函数检查文件是否存在于blob存储中 - Check if file exists in blob storage using python azure functions 如何使用 Python 从 Azure Blob 容器读取文件 - How to read a file from Azure Blob Container using Python 从 Azure Blob 存储读取 XML 文件 - Read an XML file from Azure Blob Storage 如何使用 Python delta-rs 从 Azure Blob 存储中读取数据 - How to read from Azure Blob Storage with Python delta-rs 事件网格触发器 Azure 具有 Blob 存储输入和 output 的函数 Python - Event Grid Trigger Azure Functions with blob storage input and output Python 如何直接从 Azure blob 存储读取文本文件而不将其下载到本地文件(使用 python)? - How can I read a text file from Azure blob storage directly without downloading it to a local file(using python)? 使用 python 将文件上传到 azure blob 存储 - Uploading file to azure blob storage using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM