Defina e consulte as 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 mais informações sobre as regiões e endpoints do OSS, consulte Regions and endpoints.
Neste tópico, as credenciais de acesso são obtidas a partir de variáveis de ambiente. Para mais informações sobre como configurar credenciais de acesso, consulte Configure access credentials using OSS SDK for Python 1.0.
Este tópico demonstra a criação de uma instância OSSClient com um endpoint do OSS. Para configurações alternativas, como usar um domínio personalizado ou se autenticar com credenciais do Security Token Service (STS), consulte Initialization.
Para definir a ACL de um bucket, você precisa da permissão
oss:PutBucketAcl. Para consultar a ACL de um bucket, você precisa da permissãooss:GetBucketAcl. Para mais informações, consulte Grant a custom policy.
Definir a ACL de bucket
O OSS suporta as 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 |
Somente o proprietário do bucket e usuários autorizados podem ler e gravar objetos. Outros usuários podem apenas ler objetos. 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 de bucket
O código a seguir consulta a ACL de 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 da API para configurar a ACL de um bucket, consulte PutBucketAcl.
Para mais informações sobre a operação da API para consultar a ACL de um bucket, consulte GetBucketAcl.