You can configure a progress bar to display the progress of an upload or download task.
Examples
The following code provides an example on how to use a progress bar when you use bucket.get_object_to_file to download an object:
# -*- coding: utf-8 -*-
from __future__ import print_function
import os, sys
import oss2
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console.
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName')
# If Content-Length is not configured in the HTTP response header, the value of total_bytes is None.
def percentage(consumed_bytes, total_bytes):
if total_bytes:
rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
print('\r{0}% '.format(rate), end='')
sys.stdout.flush()
# progress_callback is an optional parameter used to implement a progress bar.
bucket.get_object_to_file('yourObjectName', 'yourLocalFile', progress_callback=percentage)
References
For the complete sample code of progress bars in object download, visit GitHub.