Defina e obtenha ACLs de bucket para controlar as permissões de leitura e gravação nos objetos de um bucket.
Observações
Neste tópico, o endpoint público da região China (Hangzhou) é utilizado. Para acessar o OSS a partir de outros serviços da Alibaba Cloud na mesma região que o OSS, use um endpoint interno. Para obter mais informações sobre regiões e endpoints do OSS, consulte Regions and endpoints.
Este tópico demonstra como criar uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como uso de domínio personalizado ou autenticação com credenciais do Security Token Service (STS), consulte Initialization (C# SDK V1).
Para definir a ACL de um bucket, você precisa ter a permissão
oss:PutBucketAcl. Para obter a ACL de um bucket, você precisa ter a permissãooss:GetBucketAcl. Para obter mais informações, consulte Grant a custom policy.
Definir a ACL de um bucket
O OSS oferece suporte às seguintes ACLs de bucket.
|
ACL |
Descrição |
Método |
|
Private |
Somente o proprietário do bucket e usuários autorizados podem ler e gravar objetos. Outros usuários não têm acesso. |
CannedAccessControlList.Private |
|
Public-read |
Somente o proprietário do bucket e usuários autorizados podem ler e gravar objetos. Os demais usuários têm acesso apenas de leitura. Use com cautela. |
CannedAccessControlList.PublicRead |
|
Public-read-write |
Todos os usuários podem ler e gravar objetos no bucket. Use com cautela. |
CannedAccessControlList.PublicReadWrite |
O código a seguir mostra como definir a ACL de um bucket:
using System;
using Aliyun.OSS;
using Aliyun.OSS.Common;
namespace Samples
{
public class Program
{
public static void Main(string[] args)
{
// Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Use Signature V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Set the bucket ACL to public-read.
client.SetBucketAcl(bucketName, CannedAccessControlList.PublicRead);
Console.WriteLine("Set bucket ACL succeeded");
}
catch (Exception ex)
{
Console.WriteLine("Set bucket ACL failed. {0}", ex.Message);
}
}
}
}
Obter as permissões de acesso do bucket
O código a seguir obtém a ACL do bucket:
using System;
using Aliyun.OSS;
using Aliyun.OSS.Common;
namespace Samples
{
public class Program
{
public static void Main(string[] args)
{
// Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 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.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the bucket name. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify the default parameters as needed.
var conf = new ClientConfiguration();
// Use Signature V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OssClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
var acl = client.GetBucketAcl(bucketName);
Console.WriteLine("Get bucket ACL success {0}", acl.ACL);
}
catch (Exception ex)
{
Console.WriteLine("Get bucket ACL failed. {0}", ex.Message);
}
}
}
}
Referências
Para ver o código de exemplo completo usado na definição da ACL de um bucket, consulte o exemplo no GitHub.
Para ver o código de exemplo completo usado na recuperação da ACL de um bucket, consulte o exemplo no GitHub.
Para obter mais informações sobre a operação de API usada para definir a ACL de um bucket, consulte PutBucketAcl.
Para obter mais informações sobre a operação de API usada para recuperar a ACL de um bucket, consulte GetBucketAcl.