Todos os produtos
Search
Central de documentação

Object Storage Service:Configure download progress bars using OSS SDK for Python 1.0

Última atualização: Sep 24, 2026

Configure uma barra de progresso para acompanhar o andamento de tarefas de upload ou download.

Notes

  • Neste tópico, usa-se o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre regiões e endpoints do OSS, consulte Regions and endpoints.

  • Neste tópico, as credenciais de acesso são obtidas de variáveis de ambiente. Para saber como configurar credenciais de acesso, consulte Configure access credentials using OSS SDK for Python 1.0.

  • Este tópico demonstra a criação de uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como uso de domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Initialization.

Examples

O código de exemplo a seguir demonstra como usar uma barra de progresso ao utilizar bucket.get_object_to_file para baixar um objeto:

# -*- 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 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. 
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"

# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"

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

# If Content-Length is not in the HTTP response headers, the value of total_bytes is None. 
# consumed_bytes specifies the size of data that has been downloaded. Unit: bytes. 
# total_bytes specifies 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 specifies the download progress. 
        print('\r{0}% '.format(rate), end='')

        sys.stdout.flush()

# progress_callback is an optional parameter used to return progress information. 
bucket.get_object_to_file('yourObjectName', 'yourLocalFile', progress_callback=percentage)

References

Para acessar o código de exemplo completo sobre como configurar barras de progresso em tarefas de download de objetos, acesse o GitHub.