Todos os produtos
Search
Central de documentação

Object Storage Service:Gerencie as ACLs de objetos (SDK V2 para C#)

Última atualização: Sep 22, 2026

Este tópico descreve como usar o SDK V2 para C# para definir e obter as listas de controle de acesso (ACLs) dos objetos.

Observações

  • O código de exemplo deste tópico usa a região China (Hangzhou) (cn-hangzhou) como exemplo. Por padrão, usa-se o endpoint público. Se você quiser acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região, use um endpoint interno. Para obter mais informações sobre as regiões e os endpoints que o OSS suporta, consulte Regions and endpoints.

  • Para definir a ACL de um objeto, você deve ter a permissão oss:PutObjectAcl. Para obter a ACL de um objeto, você deve ter a permissão oss:GetObjectAcl. Para mais informações, consulte Grant custom permissions to a RAM user.

Tipos de ACL

Os objetos possuem os quatro tipos de ACL a seguir:

Permissão de acesso

Descrição

Valor da ACL

Herdar do bucket

Os arquivos herdam as permissões de acesso do bucket.

default

Privado

Somente o proprietário do objeto e os usuários autorizados têm permissões de leitura e gravação no objeto. Outros usuários não têm permissões para acessar o objeto.

private

Leitura pública

Somente o proprietário do objeto e os usuários autorizados têm permissões de leitura e gravação no objeto. Outros usuários têm apenas permissões de leitura no objeto. Use esta permissão com cautela.

public-read

Leitura e gravação públicas

Todos os usuários têm permissões de leitura e gravação no objeto. Use esta permissão com cautela.

public-read-write

A ACL de um objeto tem prioridade maior do que a ACL do bucket que o contém. Por exemplo, se a ACL de um bucket for privada e a ACL de um objeto dentro desse bucket for de leitura e gravação públicas, todos os usuários terão permissões de leitura e gravação no objeto. Caso não configure uma ACL para um objeto, ele herdará a ACL do seu bucket.

Código de exemplo

Use o código a seguir para definir e obter a ACL de um objeto.

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

var region = "cn-hangzhou";  // Required. Set the region where the bucket is located. In this example, the region is set to China (Hangzhou), and the region ID is set to cn-hangzhou.
var endpoint = null as string;  // Optional. Specify the endpoint that is used to access OSS. In this example, the endpoint is set to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your bucket name";  // Required. The name of the destination bucket.
var key = "your object name";  // Required. The name of the destination object.
var acl = "your object Acl";  // Required. The ACL of the destination object. Valid values: default, private, public-read, and public-read-write.

// Load the default configurations of the OSS SDK. The configurations automatically read credential information such as AccessKeys 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 and 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, it overwrites 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 PutObjectAclAsync method to set the ACL of the destination object.
var result1 = await client.PutObjectAclAsync(new OSS.Models.PutObjectAclRequest()
{
    Bucket = bucket,
    Key = key,
    Acl = acl,
});

// Call the GetObjectAclAsync method to obtain the ACL information of the destination object.
var result2 = await client.GetObjectAclAsync(new OSS.Models.GetObjectAclRequest()
{
    Bucket = bucket,
    Key = key,
});

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

Referências

  • Para ver o código de exemplo completo de como definir a ACL de um objeto, consulte PutObjectAcl.cs.

  • Para acessar o código de exemplo completo de como obter a ACL de um objeto, consulte GetObjectAcl.cs.