All Products
Search
Document Center

Object Storage Service:Download progress bar (Python SDK V1)

Last Updated:Aug 08, 2025

A progress bar shows the progress of an upload or download.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, 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.

Sample code

The following sample code shows how to use a progress bar with the bucket.get_object_to_file method.

# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import sys
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the region where the endpoint is located, for example, cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# If the Content-Length is not included in the HTTP header, the value of total_bytes is None.
# consumed_bytes indicates the size of the downloaded file in bytes.
# total_bytes indicates the total size of the file to be downloaded in 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='')

        sys.stdout.flush()

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

References

For the complete sample code that demonstrates how to use a progress bar for file downloads, see the GitHub example.