我目前正在为Python使用。对于我的项目,我想从特定的blob中读取/加载数据,而不必在访问之前下载/存储在磁盘上。
根据加载特定blob的文档,它适用于我的with:
blob_client = BlobClient(blob_service_client.url,
container_name,
blob_name,
credential)
data_stream = blob_client.download_blob()
data = data_stream.readall()
最后一个readall()
命令返回blob内容的字节信息(在我的例子中是图像)。
通过以下方式:
with open(loca_path, "wb") as local_file:
data_stream.readinto(my_blob)
可以将blob内容保存在磁盘上(经典下载操作)。
但是:是否也可以将data = data_stream.readall()
中的字节数据直接转换成图像?
它已经尝试了image_data = Image.frombytes(mode="RGB", data=data, size=(1080, 1920))
,但它返回了一个错误not enough image data
发布于 2022-08-03 02:02:36
下面是在不下载文件的情况下读取文本的示例代码。
from azure.storage.blob import BlockBlobService, PublicAccess
accountname="xxxx"
accountkey="xxxx"
blob_service_client = BlockBlobService(account_name=accountname,account_key=accountkey)
container_name="test2"
blob_name="a5.txt"
#get the length of the blob file, you can use it if you need a loop in your code to read a blob file.
blob_property = blob_service_client.get_blob_properties(container_name,blob_name)
print("the length of the blob is: " + str(blob_property.properties.content_length) + " bytes")
print("**********")
#get the first 10 bytes data
b1 = blob_service_client.get_blob_to_text(container_name,blob_name,start_range=0,end_range=10)
#you can use the method below to read stream
#blob_service_client.get_blob_to_stream(container_name,blob_name,start_range=0,end_range=10)
print(b1.content)
print("*******")
#get the next range of data
b2=blob_service_client.get_blob_to_text(container_name,blob_name,start_range=10,end_range=50)
print(b2.content)
print("********")
#get the next range of data
b3=blob_service_client.get_blob_to_text(container_name,blob_name,start_range=50,end_range=200)
print(b3.content)
要获得完整的信息,您可以使用Python检查https://pypi.org/project/azure-storage-blob/。
https://stackoverflow.com/questions/72869572
复制相似问题