Todos os produtos
Search
Central de documentação

Object Storage Service:Baixe um arquivo para o disco local (C# SDK V2)

Última atualização: Sep 24, 2026

Este tópico descreve como usar o método de download simples para baixar um objeto de um bucket para o disco local. Esse método é fácil de usar e ideal para baixar objetos rapidamente para o disco local.

Precauções

O código de exemplo neste tópico usa a região China (Hangzhou) (cn-hangzhou) como exemplo. Por padrão, o SDK utiliza o endpoint público. 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 os mapeamentos entre as regiões e os endpoints compatíveis com o OSS, consulte OSS regions and endpoints.

Permissões

Por padrão, uma conta Alibaba Cloud tem permissões totais. Os usuários RAM ou as funções RAM de uma conta Alibaba Cloud não têm permissões por padrão. A conta Alibaba Cloud ou o administrador da conta devem conceder as permissões de operação por meio de RAM policies ou Bucket Policy.

API

Ação

Descrição

GetObject

oss:GetObject

Baixa um objeto.

oss:GetObjectVersion

Essa permissão é obrigatória ao baixar um objeto caso especifique a versão do objeto pelo versionId.

kms:Decrypt

Essa permissão é obrigatória ao baixar um objeto se os metadados do objeto contiverem X-Oss-Server-Side-Encryption: KMS.

Código de exemplo

Use o código a seguir para baixar um objeto de um bucket para o disco local.

using OSS = AlibabaCloud.OSS.V2;  // Create an alias for the Alibaba Cloud OSS SDK to simplify subsequent use.

var region = "cn-hangzhou";  // Required. Specify the region where the bucket is located. In this example, China (Hangzhou) is used. Set the region to cn-hangzhou.
var bucket = "your bucket name";  // Required. Specify the name of the destination bucket. 
var endpoint = null as string;  // Optional. Specify the endpoint that is used to access OSS. In this example, China (Hangzhou) is used. Set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var key = "your object name";  // Required. Specify the name of the object to be downloaded. Format: folder/objectName.
var filePath = "your file path";  // Required. Specify the path of the file to be downloaded to the local disk.

// Load the default configurations of the OSS SDK. The configurations automatically read credential information (such as AccessKey) from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly set the use of environment variables to obtain credentials for identity verification. Format: OSS_ACCESS_KEY_ID, OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket in the configuration.
cfg.Region = region;   
// If an endpoint is specified, overwrite the default endpoint. 
if(endpoint != null)
{
    cfg.Endpoint = endpoint;
} 

// Create an OSS client instance using the configuration information.
using var client = new OSS.Client(cfg); 

// Call the GetObjectToFileAsync method to directly stream the OSS object to the local disk. This prevents large files from occupying too much memory.
var result = await client.GetObjectToFileAsync(new OSS.Models.GetObjectRequest()
{
    Bucket = bucket,
    Key = key,
}, filePath);

// Print the upload result.
Console.WriteLine("GetObjectToFile done");  // A message indicating that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}");  // HTTP status code.
Console.WriteLine($"RequestId: {result.RequestId}");  // The request ID, which is used for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:");  // The response header information.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));  // Traverse and print all response headers.

Referências

Para o código de exemplo completo para baixar um objeto para o disco local, consulte GetObjectToFile.cs.