python - 获取 Python 文件上传到 Azure 的进度

标签 python azure filestream

我正在将文件上传到 azure,如下所示:

with open(tempfile, "rb") as data:
    blob_client.upload_blob(data, blob_type='BlockBlob',  length=None, metadata=None)

我如何获得进度指示? 当我尝试作为流上传时,它只上传一个 block 。

我确信我做错了什么,但找不到信息。

谢谢!

最佳答案

Azure 库似乎不包含用于监视进度的回调函数。

幸运的是,您可以在 Python 的文件对象周围添加一个包装器,该包装器可以在每次读取时调用回调。

试试这个:

import os
from io import BufferedReader, FileIO


class ProgressFile(BufferedReader):
    # For binary opening only

    def __init__(self, filename, read_callback):
        f = FileIO(file=filename, mode='r')
        self._read_callback = read_callback
        super().__init__(raw=f)

        # I prefer Pathlib but this should still support 2.x
        self.length = os.stat(filename).st_size

    def read(self, size=None):
        calc_sz = size
        if not calc_sz:
            calc_sz = self.length - self.tell()
        self._read_callback(position=self.tell(), read_size=calc_sz, total=self.length)
        return super(ProgressFile, self).read(size)



def my_callback(position, read_size, total):
    # Write your own callback. You could convert the absolute values to percentages
    
    # Using .format rather than f'' for compatibility
    print("position: {position}, read_size: {read_size}, total: {total}".format(position=position,
                                                                                read_size=read_size,
                                                                                total=total))


myfile = ProgressFile(filename='mybigfile.txt', read_callback=my_callback)

那你就这么做

blob_client.upload_blob(myfile, blob_type='BlockBlob',  length=None, metadata=None)

myfile.close()

编辑: 看起来 TQDM(进度监视器)有一个简洁的包装:https://github.com/tqdm/tqdm#hooks-and-callbacks 。 这样做的好处是您可以轻松访问漂亮的进度条。

关于python - 获取 Python 文件上传到 Azure 的进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62791901/

相关文章:

python - 更新字典的键和值

azure - 从 Windows 桌面应用程序调用具有 AD 授权的 Azure Function

c# - 我的服务器程序接收到的字节不完整。为什么?我正在使用 C# .net 套接字

apache-flex - 如何使用 flash.filesystem.FileStream

Python scipy find_simplex 卡住

python - 实现需要多层关系的 django 模型的最佳方法

python - Pandas DataFrame Groupby 获取唯一行条件并识别增加值直至组数

azure - Databricks Notebook 作为 livy session 端点的替代品

azure - 如何更新 Azure Blob 存储中的文件上次修改日期

c# - System.IO.FileStream 文件访问与文件共享