This topic describes how to manage the access control lists (ACLs) of objects.
Background information
The following table describes the ACLs that you can configure for an object.
ACL | Description | Value |
Inherited from bucket | The ACL of the object is the same as the ACL of the bucket in which the object is stored. | oss2.OBJECT_ACL_DEFAULT |
Private | Only the object owner and authorized users are granted the read and write permissions on the object. | oss2.OBJECT_ACL_PRIVATE |
Public-read | Only the object owner and authorized users are granted the read and write permissions on the object. Other users are granted only the read permissions on the object. Exercise caution when you set the ACL to this value. | oss2.OBJECT_ACL_PUBLIC_READ |
Public-read-write | All users are granted the read and write permissions on the object. Exercise caution when you set the ACL to this value. | oss2.OBJECT_ACL_PUBLIC_READ_WRITE |
The ACL of an object takes precedence over the ACL of the bucket that contains the object. For example, if the ACL of a bucket is private and the ACL of an object in the bucket is public-read-write, all users are granted the read and write permissions on the object. If the ACL of an object is not configured, the ACL of the object is the same as the ACL of the bucket in which the object is stored.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions, endpoints and open ports.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.
To configure the ACL for an object, you must have the
oss:PutObjectAcl
permission. To query object ACLs, you must have theoss:GetObjectAcl
permission. For more information, see Attach a custom policy to a RAM user.
Configure the ACL of an object
The following sample code provides an example on how to configure the ACL of an object:
# -*- 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 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)
# Specify the full path of the object. Do not include the bucket name in the full path.
bucket.put_object_acl('yourObjectName', oss2.OBJECT_ACL_PUBLIC_READ)
Query the ACL of an object
The following sample code provides an example on how to query the ACL of an object:
# -*- 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 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)
# Specify the full path of the object. Do not include the bucket name in the full path.
print(bucket.get_object_acl('yourObjectName').acl)
References
For more information about the API operation that you can call to configure object ACLs, see PutObjectACL.
For more information about the API operation that you can call to query object ACLs, see GetObjectACL.