[英]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.