获取Python文件上载到Azure的进度

2024-04-26 00:36:34 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

我肯定我做错了什么,但找不到信息

谢谢


Tags: 文件clientnonedataastypewithopen
1条回答
网友
1楼 · 发布于 2024-04-26 00:36:34

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()

编辑: TQM(进度监视器)似乎有一个整洁的包装:https://github.com/tqdm/tqdm#hooks-and-callbacks。 这样做的好处是,您可以轻松访问一个相当不错的进度条

相关问题 更多 >

    热门问题