This topic describes how to manage the access control lists (ACLs) of objects in a versioned bucket.
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS by using other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about the regions and endpoints supported by OSS, see Regions and endpoints.
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 object ACLs, 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
By default, the PutObjectACL operation is called to configure the ACL of the current version of an object. If the current version of the object is a delete marker, Object Storage Service (OSS) returns 404 Not Found. You can specify a version ID in the request to configure the ACL of a specified version of an object.
The following 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.ProviderAuth(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.
# Specify the name of the bucket. Example: examplebucket.
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
# Change the ACL of the specified version of an object. In this example, the ACL of the specified version of an object is changed to public read.
params = dict()
params['versionId'] = 'yourObjectVersionId'
result = bucket.put_object_acl('yourObjectName', oss2.OBJECT_ACL_PUBLIC_READ, params = params)
# Query the version ID of the object whose ACL is changed.
print('set acl object versionid:', result.versionid)
Query the ACL of an object
By default, the GetObjectACL operation is called to query the ACL of the current version of an object. If the current version of the object is a delete marker, Object Storage Service (OSS) returns 404 Not Found. You can specify a version ID in the request to query the ACL of the specified version of an object.
The following 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.ProviderAuth(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.
# Specify the name of the bucket. Example: examplebucket.
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'examplebucket')
# Query the ACL of the specified version of an object.
params = dict()
params['versionId'] = 'yourObjectVersionId'
result = bucket.get_object_acl('yourObjectName', params = params)
# Display the ACL.
print('get object acl :', result.acl)
# Display the version ID of the object whose ACL is queried.
print('object version id:', result.versionid)
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.