Una directiva de bucket permite autorizar o restringir el acceso de usuarios anónimos u otros usuarios, como cuentas de Alibaba Cloud, usuarios de RAM y roles de RAM, a recursos específicos de Object Storage Service (OSS). Por ejemplo, puede otorgar permisos de solo lectura sobre recursos específicos de OSS a un usuario de RAM de otra cuenta de Alibaba Cloud.
Notas
Antes de configurar una directiva de bucket, asegúrese de comprender esta función. Para obtener más información, consulte Bucket Policy.
En este tema se utiliza el endpoint público de la región China (Hangzhou). Si desea acceder a OSS from otros servicios de Alibaba Cloud en la misma región, utilice un endpoint interno. Para obtener más información sobre las regiones y endpoints de OSS, consulte Regions and endpoints.
En este tema, las credenciales de acceso se obtienen de variables de entorno. Para obtener más información sobre cómo configurar las credenciales de acceso, 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 otras configuraciones, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization.
Para establecer una directiva de bucket, debe tener el permiso
oss:PutBucketPolicy. Para obtener una directiva de bucket, debe tener el permisooss:GetBucketPolicy. Para eliminar una directiva de bucket, debe tener el permisooss:DeleteBucketPolicy. Para obtener más información, consulte Grant a custom policy.
Establecer una directiva de bucket
El siguiente código de ejemplo muestra cómo establecer una directiva de bucket:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import json
# Obtain access credentials from environment variables. Before running this code, ensure the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Set the Endpoint to the URL of the region where your bucket is located. For example, for the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Set the region that corresponds to your Endpoint, such as cn-hangzhou. This parameter is required for v4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# In this example, the resource owner (bucket owner with UID 174649585760xxxx) uses a bucket policy to grant a Resource Access Management (RAM) user (UID 20214760404935xxxx) permission to list all files in the examplebucket.
policy_text = '{"Statement": [{"Effect": "Allow", "Action": ["oss:GetObject", "oss:ListObjects"], "Principal": ["20214760404935xxxx"], "Resource": ["acs:oss:*:174649585760xxxx:examplebucket/*"]}], "Version": "1"}'
# Upload the authorization policy.
bucket.put_bucket_policy(policy_text)
Obtener una directiva de bucket
El siguiente código de ejemplo muestra cómo obtener una directiva de bucket:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
import json
# Get access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the Endpoint for your bucket's region. For example, if your bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region that corresponds to the Endpoint, such as cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Get the bucket policy.
result = bucket.get_bucket_policy()
policy_json = json.loads(result.policy)
print("Get policy text: ", policy_json)
Eliminar una directiva de bucket
El siguiente código de ejemplo muestra cómo eliminar una directiva de bucket:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Get access credentials from environment variables. Before running this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Set the Endpoint for 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.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Set the region that corresponds to the Endpoint, such as cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Delete the bucket policy.
result = bucket.delete_bucket_policy()
assert int(result.status)//100 == 2
Referencias
Para ver un ejemplo completo de código de una directiva de bucket, consulte el ejemplo de GitHub.
Para consultar la referencia de la API para establecer una directiva de bucket, consulte PutBucketPolicy.
Para consultar la referencia de la API para obtener una directiva de bucket, consulte GetBucketPolicy.
Para consultar la referencia de la API para eliminar una directiva de bucket, consulte DeleteBucketPolicy.