Python Azure Blobstore:我如何读取文件而不必先下载整个文件?
我正在尝试找出如何从Azure blob存储中读取文件 通过研究它的文档,我可以看到该方法似乎是访问blob的主要方式 不过,这种方法似乎需要将整个blob下载到文件或其他流中Python Azure Blobstore:我如何读取文件而不必先下载整个文件?,python,azure,azure-storage-blobs,azure-blob-storage,Python,Azure,Azure Storage Blobs,Azure Blob Storage,我正在尝试找出如何从Azure blob存储中读取文件 通过研究它的文档,我可以看到该方法似乎是访问blob的主要方式 不过,这种方法似乎需要将整个blob下载到文件或其他流中 是否可以将Azure Blob存储中的文件作为流逐行从服务中读取?(无需先下载全部内容)接受的答案可能对您有用。可以找到文档。更新0710: 在最新的SDK中,我们也可以通过使用下载\u blob来做同样的事情 下载\u blob的源代码截图: 因此,只需提供一个偏移量和长度参数,如下所示(它根据我的测试工作): 原
是否可以将Azure Blob存储中的文件作为流逐行从服务中读取?(无需先下载全部内容)接受的答案可能对您有用。可以找到文档。更新0710: 在最新的SDK中,我们也可以通过使用
下载\u blob
来做同样的事情
下载\u blob的源代码截图
:
因此,只需提供一个偏移量
和长度
参数,如下所示(它根据我的测试工作):
原始答案: 不能逐行读取blob文件,但可以按字节读取。就像第一次读取10个字节的数据一样,接下来可以继续读取接下来的10到20个字节等 这仅在的旧版本中可用。按如下方式安装:
pip install azure-storage-blob==2.1.0
下面是示例代码(我在这里阅读了文本,但您可以将其更改为使用get\u blob\u to\u stream(container\u name,blob\u name,start\u range=0,end\u range=10)
方法来读取流):
这仍然要求我们在能够阅读其单独的行之前完整地下载blob。
pip install azure-storage-blob==2.1.0
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)