首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于Python的Azure SDK :读取blobs而不下载

用于Python的Azure SDK :读取blobs而不下载
EN

Stack Overflow用户
提问于 2022-07-05 12:37:52
回答 1查看 335关注 0票数 0

我目前正在为Python使用。对于我的项目,我想从特定的blob中读取/加载数据,而不必在访问之前下载/存储在磁盘上。

根据加载特定blob的文档,它适用于我的with:

代码语言:javascript
运行
复制
blob_client = BlobClient(blob_service_client.url,
                         container_name,
                         blob_name,
                         credential)

data_stream = blob_client.download_blob()
data = data_stream.readall()

最后一个readall()命令返回blob内容的字节信息(在我的例子中是图像)。

通过以下方式:

代码语言:javascript
运行
复制
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

EN

回答 1

Stack Overflow用户

发布于 2022-08-03 02:02:36

下面是在不下载文件的情况下读取文本的示例代码。

代码语言:javascript
运行
复制
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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72869572

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档