Use o C# SDK V2 para definir e obter a lista de controle de acesso (ACL) de um bucket, contêiner usado para armazenar objetos.
Precauções
O código de exemplo usa a região China (Hangzhou) (
cn-hangzhou) e, por padrão, um endpoint público. Para acessar o OSS a partir de outros produtos da Alibaba Cloud na mesma região, use um endpoint interno. Para mais informações sobre os endpoints de cada região, consulte Regiões e endpoints do OSS.Para definir a ACL de um bucket, você precisa da permissão
oss:PutBucketAcl. Para obter a ACL, é necessária a permissãooss:GetBucketAcl. Para saber mais, consulte Conceder permissões personalizadas a um usuário RAM.
Tipos de ACL de bucket
O OSS oferece suporte aos seguintes tipos de ACL de bucket:
|
ACL |
Descrição |
Valor da permissão |
|
private |
O proprietário do bucket e os usuários autorizados têm permissões de leitura e gravação nos objetos do bucket. Outros usuários não podem executar operações nesses objetos. |
private |
|
public-read |
O proprietário do bucket e os usuários autorizados têm permissões de leitura e gravação nos objetos do bucket. Outros usuários têm apenas permissão de leitura. Use essa permissão com cautela. |
public-read |
|
public-read-write |
Todos os usuários têm permissões de leitura e gravação nos objetos do bucket. Use essa permissão com cautela. |
public-read-write |
Código de exemplo
O código de exemplo a seguir mostra como definir e obter as ACLs de bucket.
using OSS = AlibabaCloud.OSS.V2; // Create an alias for Alibaba Cloud OSS SDK to simplify subsequent use.
var region = "cn-hangzhou"; // Required. The region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou.
var endpoint = null as string; // Optional. The endpoint used to access OSS. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your bucket name"; // Required. The bucket name.
var acl = "your bucket Acl"; // Required. The ACL of the destination bucket. Valid values: private, public-read, and public-read-write.
// 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 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, 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 PutBucketAclAsync method to set the ACL of the destination bucket.
var result1 = await client.PutBucketAclAsync(new OSS.Models.PutBucketAclRequest()
{
Bucket = bucket,
Acl = acl
});
// Call the GetBucketAclAsync method to obtain the ACL of the destination bucket.
var result2 = await client.GetBucketAclAsync(new OSS.Models.GetBucketAclRequest()
{
Bucket = bucket
});
// Print the result information.
Console.WriteLine("PutBucketAcl done"); // A message indicating that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}"); // The 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.
Console.WriteLine($"Acl: {result2.AccessControlPolicy!.AccessControlList!.Grant}"); // Print the ACL of the destination bucket.
Referências
Para ver o código de exemplo completo para definir ACLs de bucket, consulte PutBucketAcl.cs.
Para ver o código de exemplo completo para obter ACLs de bucket, consulte GetBucketAcl.cs.