Todos os produtos
Search
Central de documentação

Object Storage Service:Baixe objetos como arquivos usando o OSS SDK for Python 1.0

Última atualização: Sep 22, 2026

Baixe um objeto do Object Storage Service (OSS) de um bucket para um arquivo local.

Observações

  • Este tópico usa o endpoint público da região China (Hangzhou). Para acessar o OSS a partir de outros serviços do 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.

  • Este tópico obtém as credenciais de acesso a partir de variáveis de ambiente. Para saber mais sobre como configurar credenciais de acesso, consulte Configure access credentials using OSS SDK for Python 1.0.

  • Este tópico demonstra como criar uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como usar um domínio personalizado ou autenticar com credenciais do Security Token Service (STS), consulte Initialization.

Código de exemplo

O exemplo a seguir baixa exampleobject.txt do diretório testfolder do examplebucket para D:\localpath\examplefile.txt:

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider

# Create a server object.
# 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 for 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)

# Specify the full path of the object. Do not include the bucket name in the full path. Example: testfolder/exampleobject.txt. 
# Download the object to your local computer and save the object to the D:\\localpath\\examplefile.txt path. If a file that has the same name already exists, the downloaded object overwrites the file. If no file that has the same name exists, the downloaded object is saved in the path. 
bucket.get_object_to_file('testfolder/exampleobject.txt', 'D:\\localpath\\examplefile.txt')        

print("Download complete.")

Referências

  • Para acessar o código de exemplo completo usado para baixar um objeto como arquivo local, visite o GitHub.

  • Para mais detalhes sobre a operação de API para baixar um objeto como arquivo local, consulte GetObject.