gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:35:32 26 4
gpt4 key购买 nike

我正在将文件上传到 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com