Use the OSS SDK for Python V1 to set and retrieve access control lists (ACLs) for objects in a versioning-enabled bucket. Both operations target the current version by default; pass versionId to act on a specific version.
Prerequisites
Before you begin, ensure that you have:
A versioning-enabled OSS bucket
The
oss:PutObjectAclpermission to set object ACLsThe
oss:GetObjectAclpermission to retrieve object ACLsAccess credentials stored in the
OSS_ACCESS_KEY_IDandOSS_ACCESS_KEY_SECRETenvironment variables
The examples use the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. To create an OSSClient instance using a custom domain name or Security Token Service (STS), see Initialization.
ACL values
The following ACL values are available for objects:
| SDK constant | ACL value | Access |
|---|---|---|
oss2.OBJECT_ACL_DEFAULT | default | Inherits the bucket ACL |
oss2.OBJECT_ACL_PRIVATE | private | Only the object owner has read/write access |
oss2.OBJECT_ACL_PUBLIC_READ | public-read | The object owner has read/write access; all users have read access |
oss2.OBJECT_ACL_PUBLIC_READ_WRITE | public-read-write | All users have read/write access |
Set an object ACL
PutObjectACL sets the ACL for the current version of an object. If the current version is a delete marker, OSS returns a 404 Not Found error. To set the ACL for a specific version, include versionId in the request parameters.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Load access credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Replace with the endpoint for your bucket's region.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Required for V4 signatures.
region = "cn-hangzhou"
bucket = oss2.Bucket(auth, endpoint, "<your-bucket-name>", region=region)
# Set the ACL for a specific version of an object.
params = {"versionId": "<your-object-version-id>"}
result = bucket.put_object_acl("<your-object-name>", oss2.OBJECT_ACL_PUBLIC_READ, params=params)
print("Version ID of the object whose ACL was updated:", result.versionid)Replace the following placeholders:
| Placeholder | Description | Example |
|---|---|---|
<your-bucket-name> | Name of your OSS bucket | my-bucket |
<your-object-name> | Key of the object | photos/image.jpg |
<your-object-version-id> | Version ID of the object version to target | CAEQARiBgID... |
Get an object ACL
GetObjectACL retrieves the ACL for the current version of an object. If the current version is a delete marker, OSS returns a 404 Not Found error. To retrieve the ACL for a specific version, include versionId in the request parameters.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Load access credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this code.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Replace with the endpoint for your bucket's region.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Required for V4 signatures.
region = "cn-hangzhou"
bucket = oss2.Bucket(auth, endpoint, "<your-bucket-name>", region=region)
# Retrieve the ACL for a specific version of an object.
params = {"versionId": "<your-object-version-id>"}
result = bucket.get_object_acl("<your-object-name>", params=params)
print("ACL:", result.acl)
print("Version ID:", result.versionid)Replace the following placeholders:
| Placeholder | Description | Example |
|---|---|---|
<your-bucket-name> | Name of your OSS bucket | my-bucket |
<your-object-name> | Key of the object | photos/image.jpg |
<your-object-version-id> | Version ID of the object version to target | CAEQARiBgID... |
What's next
PutObjectACL — API reference for setting object ACLs
GetObjectACL — API reference for retrieving object ACLs
Configure access credentials — Set up environment variables for OSS SDK for Python V1
Attach a custom policy to a RAM user — Grant
oss:PutObjectAclandoss:GetObjectAclpermissions