如何使用 StorageStreamDownloader 从 blob 流式下载并流式上传到不同的 blob?
How can one use the StorageStreamDownloader to stream download from a blob and stream upload to a different blob?
我相信我有一个非常简单的需求,但解决方案让我感到困惑。我是 azure-python-sdk 的新手,使用它的新 blob 流功能收效甚微。
一些上下文
我已经使用 Java SDK 好几年了。每个 CloudBlockBlob object has a BlobInputStream and a BlobOutputStream object. When a BlobInputStream
is opened, one can invoke its many functions (most notably its read() function) to retrieve data in a true-streaming fashion. A BlobOutputStream
, once retrieved, has a write(byte[] data) function where one can continuously write data as frequently as they want until the close() 函数都会被调用。所以,我很容易做到:
- 获取一个
CloudBlockBlob
对象,打开它的 BlobInputStream
并基本上取回 'tied' 到 CloudBlockBlob
的 InputStream
。它通常维护 4MB 的数据——至少,我是这么理解的。当从其缓冲区中读取一定数量的数据时,会引入新的(相同数量的)数据,因此它始终具有大约 4MB 的新数据(直到检索到所有数据)。
- 对该数据执行一些操作。
- 检索我正在上传的
CloudBlockBlob
对象,获取它的 BlobOutputStream
,并将我对其进行一些操作的数据写入其中。
一个很好的例子就是我想压缩一个文件。我有一个 GzipStreamReader
class 可以接受 BlobInputStream
和 BlobOutputStream
。它将从 BlobInputStream
中读取数据,并在压缩一定量的数据后写入 BlobOutputStream
。它可以根据需要多次调用 write();当它完成一天的读取后,它会关闭输入和输出流,一切都很好。
现在 Python
现在,Python SDK 有点不同,这显然是有充分理由的; io
模块的工作方式不同于 Java 的 InputStream
和 OutputStream
classes(Blob{Input/Output}Stream
classes 继承自. 我一直在努力理解流式传输在 Azure 的 python SDK 中是如何真正工作的。首先,我只是想看看 StorageStreamDownloader class works. It seems like the StorageStreamDownloader
is what holds the 'connection' to the BlockBlob
object I am reading data from. If I want to put the data in a stream, I would make a new io.BytesIO()
and pass that stream to the StorageStreamDownloader
's readinto 方法是如何工作的。
对于上传,我会调用 BlobClient's upload method。上传方法接受类型为 Union[Iterable[AnyStr], IO[AnyStr]]
.
的 data
参数
我不想把我所理解的讲得太详细,因为我所理解的和我所做的都让我无处可去。我怀疑我期待只有 Java SDK 提供的东西。但是,总的来说,这是我遇到的问题:
- 当我调用 download_blob 时,我返回一个
StorageStreamDownloader
,其中包含 blob 中的所有数据。一些调查表明我可以使用 offset
和 length
来下载我想要的数据量。也许我可以用 download_blob(offset=0, length=4MB)
调用一次,处理我返回的数据,然后再次调用 download_bloc(offset=4MB, length=4MB)
,处理数据,等等。这是不利的。我可以做的另一件事是利用 BlobClient
的 max_chunk_get_size
参数并打开 validate_content
标志(使其为真),以便 StorageStreamDownloader
仅下载 4mb。但这一切都会导致几个问题:这并不是真正从 stream
对象流出的。我仍然需要多次调用 download
和 readinto
。好吧,如果不是因为第二个问题,我会这样做:
- 我到底如何流式传输上传的内容?上传可以采用流。但是如果流不自动更新自己,那么我只能上传一次,因为我处理的所有 blob 必须 是
BlockBlobs
。 upload_function
函数的文档说我可以提供一个参数 overwrite
来做:
keyword bool overwrite: Whether the blob to be uploaded should overwrite the current data.
If True, upload_blob will overwrite the existing data. If set to False, the
operation will fail with ResourceExistsError. The exception to the above is with Append
blob types: if set to False and the data already exists, an error will not be raised
and the data will be appended to the existing blob. If set overwrite=True, then the existing
append blob will be deleted, and a new one created. Defaults to False.
- 这是有道理的,因为
BlockBlobs
,一旦写入,就无法再次写入。所以 AFAIK,你不能 'stream' 上传。如果我不能有一个直接绑定到 blob 或保存所有数据的流对象,那么 upload() 函数将在完成后立即终止,对吗?
好的。我确定我错过了一些重要的东西。对于Python中的io
模块,我也有些懵。虽然我在 Python 中开发了很长时间,但我从来没有真正需要与那个模块打交道。我确信我遗漏了一些东西,因为这个功能是非常基本的并且存在于我所知道的所有其他 azure SDK 中。
回顾一下
上面说的老老实实可以忽略,只看这一段;我只是想表明我已经做了一些尽职调查。我想知道如何从 blob 流式传输数据,处理流中获取的数据,然后上传该数据。我无法一次接收 all blob 中的数据。 Blob 可能超过 1GB 以及所有漂亮的东西。老实说,我会喜欢一些显示的示例代码:
- 从流中的 blob 中检索一些数据(一次调用接收的数据不应超过 10MB)。
- 正在压缩该流中的数据。
- 将数据上传到 blob。
这应该适用于所有大小的斑点;无论是 1MB 还是 10MB 还是 10GB 都不重要。第 2 步可以是任何东西;它也可以什么都不是。只要数据被下载、插入到流中,然后上传,那就太好了。当然,另一个极其重要的限制是每个 'download' 的数据量不应超过 10MB。
我希望这是有道理的!我只想流式传输数据。这不应该那么难。
编辑:
有些人可能想关闭此问题并声称该问题是重复的。我忘了包括一些非常重要的东西:我目前使用的是 最新,不是最新的 azure-sdk 版本。我的 azure-storage-blob
包的版本是 12.5.0
。还有其他问题类似于我对严重 过时版本的要求。我搜索了其他答案,但没有找到 12+
版本的答案。
如果要分块下载azure blob,处理每一个chunk数据并将每一个chunk数据上传到azure blob,请参考下面的代码
import io
import os
from azure.storage.blob import BlobClient, BlobBlock
import uuid
key = '<account key>'
source_blob_client = BlobClient(account_url='https://andyprivate.blob.core.windows.net',
container_name='',
blob_name='',
credential=key,
max_chunk_get_size=4*1024*1024, # the size of chunk is 4M
max_single_get_size=4*1024*1024)
des_blob_client = BlobClient(account_url='https://<account name>.blob.core.windows.net',
container_name='',
blob_name='',
credential=key)
stream = source_blob_client.download_blob()
block_list = []
#read data in chunk
for chunk in stream.chunks():
#process your data
# use the put block rest api to upload the chunk to azure storage
blk_id = str(uuid.uuid4())
des_blob_client.stage_block(block_id=blk_id, data=<the data after you process>)
block_list.append(BlobBlock(block_id=blk_id))
#use the put blobk list rest api to ulpoad the whole chunk to azure storage and make up one blob
des_blob_client.commit_block_list(block_list)
此外,如果你只是想将一个blob从存储位置复制到另一个存储位置,你可以直接使用方法start_copy_from_url
我相信我有一个非常简单的需求,但解决方案让我感到困惑。我是 azure-python-sdk 的新手,使用它的新 blob 流功能收效甚微。
一些上下文
我已经使用 Java SDK 好几年了。每个 CloudBlockBlob object has a BlobInputStream and a BlobOutputStream object. When a BlobInputStream
is opened, one can invoke its many functions (most notably its read() function) to retrieve data in a true-streaming fashion. A BlobOutputStream
, once retrieved, has a write(byte[] data) function where one can continuously write data as frequently as they want until the close() 函数都会被调用。所以,我很容易做到:
- 获取一个
CloudBlockBlob
对象,打开它的BlobInputStream
并基本上取回 'tied' 到CloudBlockBlob
的InputStream
。它通常维护 4MB 的数据——至少,我是这么理解的。当从其缓冲区中读取一定数量的数据时,会引入新的(相同数量的)数据,因此它始终具有大约 4MB 的新数据(直到检索到所有数据)。 - 对该数据执行一些操作。
- 检索我正在上传的
CloudBlockBlob
对象,获取它的BlobOutputStream
,并将我对其进行一些操作的数据写入其中。
一个很好的例子就是我想压缩一个文件。我有一个 GzipStreamReader
class 可以接受 BlobInputStream
和 BlobOutputStream
。它将从 BlobInputStream
中读取数据,并在压缩一定量的数据后写入 BlobOutputStream
。它可以根据需要多次调用 write();当它完成一天的读取后,它会关闭输入和输出流,一切都很好。
现在 Python
现在,Python SDK 有点不同,这显然是有充分理由的; io
模块的工作方式不同于 Java 的 InputStream
和 OutputStream
classes(Blob{Input/Output}Stream
classes 继承自. 我一直在努力理解流式传输在 Azure 的 python SDK 中是如何真正工作的。首先,我只是想看看 StorageStreamDownloader class works. It seems like the StorageStreamDownloader
is what holds the 'connection' to the BlockBlob
object I am reading data from. If I want to put the data in a stream, I would make a new io.BytesIO()
and pass that stream to the StorageStreamDownloader
's readinto 方法是如何工作的。
对于上传,我会调用 BlobClient's upload method。上传方法接受类型为 Union[Iterable[AnyStr], IO[AnyStr]]
.
data
参数
我不想把我所理解的讲得太详细,因为我所理解的和我所做的都让我无处可去。我怀疑我期待只有 Java SDK 提供的东西。但是,总的来说,这是我遇到的问题:
- 当我调用 download_blob 时,我返回一个
StorageStreamDownloader
,其中包含 blob 中的所有数据。一些调查表明我可以使用offset
和length
来下载我想要的数据量。也许我可以用download_blob(offset=0, length=4MB)
调用一次,处理我返回的数据,然后再次调用download_bloc(offset=4MB, length=4MB)
,处理数据,等等。这是不利的。我可以做的另一件事是利用BlobClient
的max_chunk_get_size
参数并打开validate_content
标志(使其为真),以便StorageStreamDownloader
仅下载 4mb。但这一切都会导致几个问题:这并不是真正从stream
对象流出的。我仍然需要多次调用download
和readinto
。好吧,如果不是因为第二个问题,我会这样做: - 我到底如何流式传输上传的内容?上传可以采用流。但是如果流不自动更新自己,那么我只能上传一次,因为我处理的所有 blob 必须 是
BlockBlobs
。upload_function
函数的文档说我可以提供一个参数overwrite
来做:
keyword bool overwrite: Whether the blob to be uploaded should overwrite the current data. If True, upload_blob will overwrite the existing data. If set to False, the operation will fail with ResourceExistsError. The exception to the above is with Append blob types: if set to False and the data already exists, an error will not be raised and the data will be appended to the existing blob. If set overwrite=True, then the existing append blob will be deleted, and a new one created. Defaults to False.
- 这是有道理的,因为
BlockBlobs
,一旦写入,就无法再次写入。所以 AFAIK,你不能 'stream' 上传。如果我不能有一个直接绑定到 blob 或保存所有数据的流对象,那么 upload() 函数将在完成后立即终止,对吗?
好的。我确定我错过了一些重要的东西。对于Python中的io
模块,我也有些懵。虽然我在 Python 中开发了很长时间,但我从来没有真正需要与那个模块打交道。我确信我遗漏了一些东西,因为这个功能是非常基本的并且存在于我所知道的所有其他 azure SDK 中。
回顾一下
上面说的老老实实可以忽略,只看这一段;我只是想表明我已经做了一些尽职调查。我想知道如何从 blob 流式传输数据,处理流中获取的数据,然后上传该数据。我无法一次接收 all blob 中的数据。 Blob 可能超过 1GB 以及所有漂亮的东西。老实说,我会喜欢一些显示的示例代码:
- 从流中的 blob 中检索一些数据(一次调用接收的数据不应超过 10MB)。
- 正在压缩该流中的数据。
- 将数据上传到 blob。
这应该适用于所有大小的斑点;无论是 1MB 还是 10MB 还是 10GB 都不重要。第 2 步可以是任何东西;它也可以什么都不是。只要数据被下载、插入到流中,然后上传,那就太好了。当然,另一个极其重要的限制是每个 'download' 的数据量不应超过 10MB。
我希望这是有道理的!我只想流式传输数据。这不应该那么难。
编辑:
有些人可能想关闭此问题并声称该问题是重复的。我忘了包括一些非常重要的东西:我目前使用的是 最新,不是最新的 azure-sdk 版本。我的 azure-storage-blob
包的版本是 12.5.0
。还有其他问题类似于我对严重 过时版本的要求。我搜索了其他答案,但没有找到 12+
版本的答案。
如果要分块下载azure blob,处理每一个chunk数据并将每一个chunk数据上传到azure blob,请参考下面的代码
import io
import os
from azure.storage.blob import BlobClient, BlobBlock
import uuid
key = '<account key>'
source_blob_client = BlobClient(account_url='https://andyprivate.blob.core.windows.net',
container_name='',
blob_name='',
credential=key,
max_chunk_get_size=4*1024*1024, # the size of chunk is 4M
max_single_get_size=4*1024*1024)
des_blob_client = BlobClient(account_url='https://<account name>.blob.core.windows.net',
container_name='',
blob_name='',
credential=key)
stream = source_blob_client.download_blob()
block_list = []
#read data in chunk
for chunk in stream.chunks():
#process your data
# use the put block rest api to upload the chunk to azure storage
blk_id = str(uuid.uuid4())
des_blob_client.stage_block(block_id=blk_id, data=<the data after you process>)
block_list.append(BlobBlock(block_id=blk_id))
#use the put blobk list rest api to ulpoad the whole chunk to azure storage and make up one blob
des_blob_client.commit_block_list(block_list)
此外,如果你只是想将一个blob从存储位置复制到另一个存储位置,你可以直接使用方法start_copy_from_url