A bucket policy allows you to authorize or restrict access of anonymous users or identified users, such as Alibaba Cloud accounts, RAM users, and RAM roles, to specific Object Storage Service (OSS) resources. For example, you can grant read-only permissions on specific OSS resources to a RAM user of another Alibaba Cloud account.
Usage notes
Before you configure bucket policies, make sure that you are familiar with this feature. For more information, see Bucket policies.
To configure a bucket policy, you must have the
oss:PutBucketPolicy
permission. To query bucket policies, you must have theoss:GetBucketPolicy
permission. To delete a bucket policy, you must have theoss:DeleteBucketPolicy
permission. For more information, see Attach a custom policy to a RAM user.
Configure a bucket policy
The following sample code provides an example on how to configure a bucket policy:
const OSS = require('ali-oss')
const client = new OSS({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'yourregion',
// 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.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
// Specify the name of the bucket. Example: examplebucket.
bucket: 'examplebucket'
});
// In the following example, the bucket owner whose UID is 174649585760xxxx uses a bucket policy to authorize a RAM user whose UID is 20214760404935xxxx to list all objects in the examplebucket bucket.
const policy = {
Version: '1',
Statement: [
{
Action: ['oss:ListObjects', 'oss:GetObject'],
Effect: 'Allow',
Principal: ['20214760404935xxxx'],
Resource: ['acs:oss:*:174649585760xxxx:examplebucket']
}
]
};
async function putPolicy() {
const result = await client.putBucketPolicy('examplebucket', policy);
console.log(result)
}
putPolicy()
Query bucket policies
The following sample code shows how to query bucket policies:
const OSS = require('ali-oss')
const client = new OSS({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'yourregion',
// 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.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
// Specify the name of the bucket.
bucket: 'yourbucketname'
});
// Query the configurations of the bucket policies.
async function getPolicy() {
const result = await client.getBucketPolicy('yourbucketname');
console.log(result.policy)
}
getPolicy()
Delete a bucket policy
The following sample code provides an example on how to delete a bucket policy:
const OSS = require('ali-oss')
const client = new OSS({
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: 'yourregion',
// 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.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
// Specify the name of the bucket.
bucket: 'yourbucketname'
});
// Delete the bucket policy.
async function deletePolicy() {
const result = await client.deleteBucketPolicy('yourbucketname');
console.log(result)
}
deletePolicy()
References
For the complete sample code that is used to manage bucket policies, visit GitHub.
For more information about the API operation that you can call to configure a bucket policy, see PutBucketPolicy.
For more information about the API operation that you can call to query bucket policies, see GetBucketPolicy.
For more information about the API operation that you can call to delete a bucket policy, see DeleteBucketPolicy.