[英]Read files from azure blob storage using python azure functions
[英]Read Json files from Azure blob using python?
我需要從 Azure 中的 blob 容器中讀取 JSON 文件,以便在 JSON 文件之上進行一些轉換。 我看過很少的文檔和 StackOverflow 答案,並開發了一個 python 代碼,該代碼將從 blob 中讀取文件。
我已經從 Stackoverflow 答案之一嘗試了以下腳本來讀取 JSON 文件,但出現以下錯誤
“TypeError:JSON object 必須是 str、字節或字節數組,而不是 BytesIO”
我是 python 編程的新手,所以不確定代碼中的問題。 我嘗試使用 download_stream.content_as_text() 但文件沒有讀取文件而沒有任何錯誤。
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
from io import BytesIO
import requests
from pandas import json_normalize
import json
filename = "sample.json"
container_name="test"
constr = ""
blob_service_client = BlobServiceClient.from_connection_string(constr)
container_client=blob_service_client.get_container_client(container_name)
blob_client = container_client.get_blob_client(filename)
streamdownloader=blob_client.download_blob()
stream = BytesIO()
streamdownloader.download_to_stream(stream)
# with open(stream) as j:
# contents = json.loads(j)
fileReader = json.loads(stream)
print(filereader)
您可以使用readall
function。 請嘗試以下代碼:
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
import json
filename = "sample.json"
container_name="test"
constr = ""
blob_service_client = BlobServiceClient.from_connection_string(constr)
container_client = blob_service_client.get_container_client(container_name)
blob_client = container_client.get_blob_client(filename)
streamdownloader = blob_client.download_blob()
fileReader = json.loads(streamdownloader.readall())
print(fileReader)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.