Manage bucket ACLs (Harmony SDK)
A bucket is a container for objects. You can use the Harmony SDK to set and retrieve the access control list (ACL) for a bucket.
Precautions
-
For the mappings between Object Storage Service (OSS) regions and endpoints, see Regions and Endpoints.
-
To set the ACL for a bucket, you must have the
oss:PutBucketAclpermission. To retrieve the ACL for a bucket, you must have theoss:GetBucketAclpermission. For more information, see Grant custom access policies to a RAM user.
Bucket ACL types
The following table describes the three types of bucket ACLs.
|
ACL |
Description |
Permission value |
|
Private |
The bucket owner and authorized users have read and write permissions on objects. Other users have no permissions. |
private |
|
Public-read |
The bucket owner and authorized users have read and write permissions on objects. Other users have only read permissions. Use this permission with caution. |
public-read |
|
Public-read-write |
All users have read and write permissions on objects. Use this permission with caution. |
public-read-write |
Sample code
Set a bucket ACL
import Client, { RequestError, EBucketAcl } from '@aliyun/oss';
// Create an OSS client instance.
const client = new Client({
// Replace with the Access Key ID of your Security Token Service (STS) temporary access credential.
accessKeyId: 'yourAccessKeyId',
// Replace with the Access Key Secret of your STS temporary access credential.
accessKeySecret: 'yourAccessKeySecret',
// Replace with the Security Token of your STS temporary access credential.
securityToken: 'yourSecurityToken',
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
});
/**
* Set the access control list (ACL) for a bucket.
* Use the putBucketAcl method to set the ACL for a specified bucket.
*/
const putBucketAcl = async () => {
try {
// Call the putBucketAcl method to set the ACL for the specified bucket.
const res = await client.putBucketAcl({
bucket: 'yourBucketName', // The bucket name. Replace with your actual bucket name.
acl: EBucketAcl.PRIVATE, // Set the bucket ACL. In this example, it is set to private.
});
// Print the result of setting the ACL.
console.log(JSON.stringify(res));
} catch (err) {
// Catch exceptions that occur during the request.
if (err instanceof RequestError) {
// If the error is a known type, print the error code, message, request ID, status code, and EC code.
console.log('code: ', err.code); // The error code.
console.log('message: ', err.message); // The error message.
console.log('requestId: ', err.requestId); // The request ID.
console.log('status: ', err.status); // The HTTP status code.
console.log('ec: ', err.ec); // The error code.
} else {
// Print other unknown errors.
console.log('unknown error: ', err);
}
}
};
// Call the putBucketAcl function to set the bucket ACL.
putBucketAcl();
Obtain read and write permissions for a bucket
import Client, { RequestError } from '@aliyun/oss';
// Create an OSS client instance.
const client = new Client({
// Replace with the Access Key ID of your Security Token Service (STS) temporary access credential.
accessKeyId: 'yourAccessKeyId',
// Replace with the Access Key Secret of your STS temporary access credential.
accessKeySecret: 'yourAccessKeySecret',
// Replace with the Security Token of your STS temporary access credential.
securityToken: 'yourSecurityToken',
// Specify the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'oss-cn-hangzhou',
});
/**
* Get the access control list (ACL) for a bucket.
* Use the getBucketAcl method to get the ACL information for a specified bucket.
*/
const getBucketAcl = async () => {
try {
// Call the getBucketAcl method to get the ACL information for the specified bucket.
const res = await client.getBucketAcl({
bucket: 'yourBucketName', // The bucket name. Replace with your actual bucket name.
});
// Print the retrieved ACL information.
console.log(JSON.stringify(res));
} catch (err) {
// Catch exceptions that occur during the request.
if (err instanceof RequestError) {
// If the error is a known type, print the error code, message, request ID, status code, and EC code.
console.log('code: ', err.code); // The error code.
console.log('message: ', err.message); // The error message.
console.log('requestId: ', err.requestId); // The request ID.
console.log('status: ', err.status); // The HTTP status code.
console.log('ec: ', err.ec); // The error code.
} else {
// Print other unknown errors.
console.log('unknown error: ', err);
}
}
};
// Call the getBucketAcl function to get the bucket ACL.
getBucketAcl();