Este tópico descreve como baixar rapidamente um objeto de um bucket do Object Storage Service (OSS) para um dispositivo local.
Notas de uso
O código de exemplo neste tópico usa o ID da região cn-hangzhou, correspondente à região China (Hangzhou). Por padrão, o acesso aos recursos de um bucket ocorre via endpoint público. Para acessar os recursos do bucket a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre as regiões e endpoints suportados pelo OSS, consulte Regiões e endpoints.
Permissões
Por padrão, uma conta Alibaba Cloud tem permissões totais. Usuários RAM ou funções RAM vinculados a essa conta não possuem permissões iniciais. A conta Alibaba Cloud ou o administrador deve conceder as permissões de operação por meio de políticas do RAM ou Bucket Policy.
|
API |
Action |
Descrição |
|
GetObject |
|
Baixa um objeto. |
|
|
Necessária ao baixar um objeto com versão específica definida via versionId. |
|
|
|
Exigida quando os metadados do objeto contêm X-Oss-Server-Side-Encryption: KMS durante o download. |
Definição do método
get_object(request: GetObjectRequest, **kwargs) → GetObjectResult
Parâmetros da solicitação
|
Parâmetro |
Tipo |
Descrição |
|
request |
GetObjectRequest |
O parâmetro da solicitação. Para mais informações, consulte GetObjectRequest |
Parâmetros de resposta
|
Tipo |
Descrição |
|
GetObjectResult |
O valor de retorno. Para detalhes, consulte GetObjectResult |
Para a definição completa do método de download simples, consulte get_object.
Código de exemplo
O código a seguir baixa um objeto para um dispositivo local:
import argparse
import alibabacloud_oss_v2 as oss
import os
# Create a command-line parameter parser.
parser = argparse.ArgumentParser(description="get object sample")
# Specify the --region parameter to indicate the region in which the bucket is located. This parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter to indicate the name of the bucket. This command line parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter to indicate the endpoint of the region in which the bucket is located. This parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Specify the --key parameter to indicate the name of the object. This parameter is required.
parser.add_argument('--key', help='The name of the object.', required=True)
def main():
# Parse the command-line parameters.
args = parser.parse_args()
# Obtain access credentials from environment variables for authentication.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default configuration of the SDK and specify the credential provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Specify the region in which the bucket is located.
cfg.region = args.region
# If an endpoint is provided, specify the endpoint in the configuration object.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the configuration to create an OSSClient instance.
client = oss.Client(cfg)
# Execute the request to download the object, and specify the bucket name and object name.
result = client.get_object(oss.GetObjectRequest(
bucket=args.bucket, # Specify the name of the bucket.
key=args.key, # Specify object key.
))
# Display the response to check whether the request is successful.
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' content length: {result.content_length},'
f' content range: {result.content_range},'
f' content type: {result.content_type},'
f' etag: {result.etag},'
f' last modified: {result.last_modified},'
f' content md5: {result.content_md5},'
f' cache control: {result.cache_control},'
f' content disposition: {result.content_disposition},'
f' content encoding: {result.content_encoding},'
f' expires: {result.expires},'
f' hash crc64: {result.hash_crc64},'
f' storage class: {result.storage_class},'
f' object type: {result.object_type},'
f' version id: {result.version_id},'
f' tagging count: {result.tagging_count},'
f' server side encryption: {result.server_side_encryption},'
f' server side data encryption: {result.server_side_data_encryption},'
f' next append position: {result.next_append_position},'
f' expiration: {result.expiration},'
f' restore: {result.restore},'
f' process status: {result.process_status},'
f' delete marker: {result.delete_marker},'
)
# ========== Method 1: Read the entire object ==========
with result.body as body_stream:
data = body_stream.read()
print(f"The object is read. Data length: {len(data)} bytes")
path = "./get-object-sample.txt"
with open(path, 'wb') as f:
f.write(data)
print(f"The object is downloaded and saved to a local path: {path}")
# # ========== Method 2: Read the object in chunks ==========
# with result.body as body_stream:
# chunk_path = "./get-object-sample-chunks.txt"
# total_size = 0
# with open(chunk_path, 'wb') as f:
# # Use 256KB block size (you can adjust the block_size parameter as needed)
# for chunk in body_stream.iter_bytes(block_size=256 * 1024):
# f.write(chunk)
# total_size += len(chunk)
# print(f"Received data block: {len(chunk)} bytes | Total: {total_size} bytes")
# print(f"The object is downloaded and saved to the local path: {chunk_path}")
# Call the main function when the script is directly run.
if __name__ == "__main__":
main() # The entry point of the script. When the script is directly run, the main function is called.
Cenários comuns
Download condicional
Exibir progresso do download
Baixar objetos em lote para arquivos locais
Referência
Para o código de exemplo completo sobre download simples, consulte get_object.py e get_object_to_file.py.