Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show progress bar when we are downloading a file from cloud bucket using python

I am downloading a zip file from the cloud bucket using below python code

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

How to show progress bar after printing the line "Downloading File"

like image 367
Sam Avatar asked Jul 09 '20 09:07

Sam


Video Answer


2 Answers

Fairly simple with the tqdm library.

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)
like image 97
potatochip Avatar answered Oct 19 '22 20:10

potatochip


Maybe someone else needs something like this. Here is my solution, while I've used Greenstick answer for the progress bar.

#!/usr/bin/env python

import os
from google.cloud.storage import client as storage_client

# Print iterations progress
def printProgressBar(iteration, total, prefix = '', suffix = '', decimals = 1, fill = '█', printEnd = "\r"):
    """
    Call in a loop to create terminal progress bar
    @params:
        iteration   - Required  : current iteration (Int)
        total       - Required  : total iterations (Int)
        prefix      - Optional  : prefix string (Str)
        suffix      - Optional  : suffix string (Str)
        decimals    - Optional  : positive number of decimals in percent complete (Int)
        fill        - Optional  : bar fill character (Str)
        printEnd    - Optional  : end character (e.g. "\r", "\r\n") (Str)
    """
    length = os.get_terminal_size().columns - 12
    percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
    filledLength = int(length * iteration // total)
    bar = fill * filledLength + '-' * (length - filledLength)
    print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
    # Print New Line on Complete
    if iteration == total:
        print()

def getBucketFileProgress(bucket_name, source_blob_name, destination_file_name, chunk_size=262144*5, client=None):
    """
    Downloads a file from a bucket with a progres bar.
    @params: 
        bucket_name           - Required : bucket name (Str)
        source_blob_name      - Required : blob name (Str)
        destination_file_name - Required : local filename (Str)
        chunk_size            - Optional : Must be a multiple of 256 KB. Default 5*256KB (Int)
        client                - Optional : storage client (Obj)
    """
    if not client:
        client = storage_client.Client()

    bucket = client.get_bucket(bucket_name)
    blob = bucket.get_blob(source_blob_name)
    parts = int(blob.size / chunk_size) 
    file_obj = open(destination_file_name, "wb")
    for part in range(0,parts+1):
        blob.download_to_file(file_obj, start=part*chunk_size, end=(part+1)*chunk_size-1)
        printProgressBar(part, parts)
    print('Download completed')
like image 2
organicData Avatar answered Oct 19 '22 19:10

organicData