Defina e obtenha ACLs de bucket para controlar as permissões de leitura e gravação dos objetos em um bucket.
Observações
Este tópico utiliza o endpoint público da região China (Hangzhou). Para 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 regiões e endpoints do OSS, consulte Regiões e endpoints.
As credenciais de acesso neste tópico são obtidas de variáveis de ambiente. Para saber como configurar credenciais de acesso, consulte Configurar credenciais de acesso (Python SDK V1).
Este tópico demonstra a criação de 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 Inicialização.
Para definir a ACL de um bucket, é necessária a permissão
oss:PutBucketAcl. Para obter a ACL, é necessária a permissãooss:GetBucketAcl. Para mais informações, consulte Conceder uma política personalizada.
Definir a ACL do 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. |
oss2.BUCKET_ACL_PRIVATE |
|
Public-read |
Apenas o proprietário do bucket e usuários autorizados podem ler e gravar objetos. Demais usuários têm permissão somente para leitura. Use com cautela. |
oss2.BUCKET_ACL_PUBLIC_READ |
|
Public-read-write |
Todos os usuários podem ler e gravar objetos no bucket. Use com cautela. |
oss2.BUCKET_ACL_PUBLIC_READ_WRITE |
O código de exemplo a seguir mostra como definir a ACL de um bucket:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Set the ACL of the bucket to private.
bucket.put_bucket_acl(oss2.BUCKET_ACL_PRIVATE)
Consultar a ACL do bucket
O código a seguir obtém a ACL do bucket:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from the environment variables. Before you run the sample code, make sure that you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Query the ACL of the bucket.
print(bucket.get_bucket_acl().acl)
Referências
O código de exemplo completo para gerenciar ACLs de bucket está disponível no GitHub.
Para mais informações sobre a operação de API para configurar a ACL de um bucket, consulte PutBucketAcl.
Para mais informações sobre a operação de API para consultar a ACL de um bucket, consulte GetBucketAcl.