Sam*_*Sam 5 python google-cloud-storage google-cloud-platform
我正在使用下面的 python 代码从云存储桶下载一个 zip 文件
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(source_blob_name)
print('Downloading file')
blob.download_to_filename(destination_file_name)
print('Download completed')
Run Code Online (Sandbox Code Playgroud)
打印“下载文件”行后如何显示进度条
小智 10
使用 tqdm 库相当简单。
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(source_blob_name)
with open(destination_file_name, 'wb') as f:
with tqdm.wrapattr(f, "write", total=blob.size) as file_obj:
# blob.download_to_file is deprecated
storage_client.download_blob_to_file(blob, file_obj)
Run Code Online (Sandbox Code Playgroud)
小智 2
也许其他人需要这样的东西。\n这是我的解决方案,而我已经使用Greenstick 答案作为进度条。
\n#!/usr/bin/env python\n\nimport os\nfrom google.cloud.storage import client as storage_client\n\n# Print iterations progress\ndef printProgressBar(iteration, total, prefix = \'\', suffix = \'\', decimals = 1, fill = \'\xe2\x96\x88\', printEnd = "\\r"):\n """\n Call in a loop to create terminal progress bar\n @params:\n iteration - Required : current iteration (Int)\n total - Required : total iterations (Int)\n prefix - Optional : prefix string (Str)\n suffix - Optional : suffix string (Str)\n decimals - Optional : positive number of decimals in percent complete (Int)\n fill - Optional : bar fill character (Str)\n printEnd - Optional : end character (e.g. "\\r", "\\r\\n") (Str)\n """\n length = os.get_terminal_size().columns - 12\n percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))\n filledLength = int(length * iteration // total)\n bar = fill * filledLength + \'-\' * (length - filledLength)\n print(f\'\\r{prefix} |{bar}| {percent}% {suffix}\', end = printEnd)\n # Print New Line on Complete\n if iteration == total:\n print()\n\ndef getBucketFileProgress(bucket_name, source_blob_name, destination_file_name, chunk_size=262144*5, client=None):\n """\n Downloads a file from a bucket with a progres bar.\n @params: \n bucket_name - Required : bucket name (Str)\n source_blob_name - Required : blob name (Str)\n destination_file_name - Required : local filename (Str)\n chunk_size - Optional : Must be a multiple of 256 KB. Default 5*256KB (Int)\n client - Optional : storage client (Obj)\n """\n if not client:\n client = storage_client.Client()\n\n bucket = client.get_bucket(bucket_name)\n blob = bucket.get_blob(source_blob_name)\n parts = int(blob.size / chunk_size) \n file_obj = open(destination_file_name, "wb")\n for part in range(0,parts+1):\n blob.download_to_file(file_obj, start=part*chunk_size, end=(part+1)*chunk_size-1)\n printProgressBar(part, parts)\n print(\'Download completed\')\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
456 次 |
最近记录: |