Use barras de progresso e visualize o progresso de upload de objetos.
Observações
Este tópico utiliza o endpoint público da região China (Hangzhou). Se você quiser acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região do OSS, use um endpoint interno. Para obter mais informações sobre as regiões e endpoints do OSS, consulte Regions and endpoints.
Este tópico obtém as credenciais de acesso a partir de variáveis de ambiente. Para obter mais informações, configure as credenciais de acesso conforme descrito em 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 o uso de um domínio personalizado ou a autenticação com credenciais do Security Token Service (STS), consulte Initialization.
Exemplos
O código de exemplo a seguir demonstra como usar uma barra de progresso ao utilizar bucket.put_object para fazer upload de 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)
# consumed_bytes specifies the size of data that has been uploaded.
# total_bytes specifies the total size of data that you want to upload. If the size of the data that you want to upload cannot be determined, the value of the total_bytes parameter 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 return progress information.
bucket.put_object('yourObjectName', 'a'*1024*1024, progress_callback=percentage)
Referências
Para acessar o código de exemplo completo sobre como usar uma barra de progresso durante o upload de objetos, visite o GitHub.