En este tema se describe cómo gestionar los permisos de acceso a archivos.
Contexto
Los archivos admiten los cuatro tipos de listas de control de acceso (ACL) a continuación:
|
Permiso de acceso |
Descripción |
Valor de ACL |
|
Inherit from bucket |
El archivo hereda la ACL del bucket. |
oss2.OBJECT_ACL_DEFAULT |
|
Private |
Solo el propietario del archivo y los usuarios autorizados tienen permisos de lectura y escritura sobre él. Los demás usuarios no pueden acceder al archivo. |
oss2.OBJECT_ACL_PRIVATE |
|
Public-read |
El propietario del archivo y los usuarios autorizados tienen permisos de lectura y escritura sobre él. Los demás usuarios solo cuentan con permisos de lectura. Use este permiso con precaución. |
oss2.OBJECT_ACL_PUBLIC_READ |
|
Public-read-write |
Todos los usuarios tienen permisos de lectura y escritura sobre el archivo. Use este permiso con precaución. |
oss2.OBJECT_ACL_PUBLIC_READ_WRITE |
La ACL de un archivo tiene prioridad sobre la ACL del bucket. Por ejemplo, si un bucket es privado pero uno de sus archivos tiene la configuración public-read-write, todos los usuarios tendrán permisos de lectura y escritura sobre dicho archivo. Si un archivo no cuenta con una ACL, hereda la del bucket.
Notas
En este tema se utiliza el endpoint público de la región China (Hangzhou). Si desea acceder a OSS desde otros servicios de Alibaba Cloud ubicados en la misma región que OSS, use 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 se obtienen a partir 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 conocer configuraciones alternativas, como usar un dominio personalizado o autenticarse con credenciales de Security Token Service (STS), consulte Initialization.
Para configurar la ACL de un objeto, necesita el permiso
oss:PutObjectAcl. Para consultar las ACL de los objetos, requiere el permisooss:GetObjectAcl. Para obtener más información, consulte Grant a custom policy.
Configuración de permisos de acceso a archivos
A continuación se muestra un ejemplo de código para configurar la ACL de un objeto:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify 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"
# Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Set yourObjectName to the full path of the object. The full path cannot contain the bucket name.
bucket.put_object_acl('yourObjectName', oss2.OBJECT_ACL_PUBLIC_READ)
Consulta de permisos de acceso a archivos
A continuación se muestra un ejemplo de código para consultar la ACL de un objeto:
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify 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"
# Specify the region where the bucket is located, such as cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Set yourObjectName to the full path of the object. The full path cannot contain the bucket name.
print(bucket.get_object_acl('yourObjectName').acl)
Referencias
Para obtener más información sobre la operación de API que permite configurar los permisos de acceso a archivos, consulte PutObjectACL.
Si necesita detalles sobre la operación de API para recuperar los permisos de acceso a archivos, consulte GetObjectACL.