All Products
Document Center

Object Storage Service:Progress bars

Last Updated:Aug 21, 2023

You can configure a progress bar to display the progress of an upload or download task.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.


The following sample 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
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# 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 
# Specify the name of the bucket. 
bucket = oss2.Bucket(auth, '', 'yourBucketName')
# If Content-Length is not configured in the HTTP response header, the value of total_bytes is None. 
# consumed_bytes indicates the size of data that has been downloaded. Unit: bytes. 
# total_bytes indicates the total size of the object that you want to download. Unit: bytes. 
def percentage(consumed_bytes, total_bytes):
    if total_bytes:
        rate = int(100 * (float(consumed_bytes) / float(total_bytes)))
        # rate indicates the download progress. 
        print('\r{0}% '.format(rate), end='')


# progress_callback is an optional parameter. The parameter is used to implement a progress bar. 
bucket.get_object_to_file('yourObjectName', 'yourLocalFile', progress_callback=percentage)


For the complete sample code for configuring progress bars for object download tasks, visit GitHub.