Establezca y obtenga las ACL de bucket para controlar los permisos de lectura y escritura sobre los objetos almacenados en un bucket.
Notas
En este tema se utiliza el endpoint público de la región China (Hangzhou). Si necesita acceder a OSS desde otros servicios de Alibaba Cloud ubicados en la misma región que OSS, utilice un endpoint interno. Para obtener más información sobre las regiones y los endpoints de OSS, consulte Regions and endpoints.
Las credenciales de acceso de este tema se obtienen a partir de variables de entorno. Para conocer cómo configurar estas credenciales, consulte Configure access credentials using OSS SDK for Python 1.0.
Este tema muestra cómo crear una instancia de OSSClient con un endpoint de OSS. Para configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization.
Para establecer la ACL de un bucket, es obligatorio contar con el permiso
oss:PutBucketAcl. Para obtener la ACL de un bucket, es obligatorio tener el permisooss:GetBucketAcl. Para obtener más información, consulte Grant a custom policy.
Establecer la ACL del bucket
OSS admite las siguientes ACL de bucket.
|
ACL |
Descripción |
Método |
|
Private |
Solo el propietario del bucket y los usuarios autorizados pueden leer y escribir objetos. El resto de los usuarios no tiene acceso. |
oss2.BUCKET_ACL_PRIVATE |
|
Public-read |
Únicamente el propietario del bucket y los usuarios autorizados leen y escriben objetos. Los demás usuarios solo pueden leerlos. Utilícelo con precaución. |
oss2.BUCKET_ACL_PUBLIC_READ |
|
Public-read-write |
Cualquier usuario lee y escribe objetos dentro del bucket. Utilícelo con precaución. |
oss2.BUCKET_ACL_PUBLIC_READ_WRITE |
A continuación se muestra un ejemplo de código para establecer la ACL de un 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 la ACL del bucket
El siguiente código obtiene la ACL del 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)
Referencias
El código de ejemplo completo para gestionar las ACL de bucket está disponible en GitHub.
Para obtener más información sobre la operación de API que configura la ACL de un bucket, consulte PutBucketAcl.
Si desea conocer más detalles sobre la operación de API que consulta la ACL de un bucket, consulte GetBucketAcl.