This topic describes how to configure, query, and delete the policies of a bucket.

Background information

Bucket policies provide resource-based authorization for users. Bucket policies apply to the following scenarios:

  • Authorize RAM users of other accounts to access your OSS resources.

    You can authorize RAM users of other accounts to access your OSS resources.

  • Authorize anonymous users to access your OSS resources using specific IP addresses or IP ranges.

    In some cases, you must authorize anonymous users to access OSS resources using specific IP addresses or IP ranges. For example, confidential documents of an enterprise are only allowed to be accessed within the enterprise but not in other regions. Previously, configuring RAM policies for every user was a tedious and complex task because of the potential for a large number of internal users. To resolve this issue, you can configure access policies with IP restrictions based on bucket policies to authorize a large number of users easily and efficiently.

For more information about bucket policy configurations and use cases, see Use bucket policies to authorize other users to access OSS resources. For more information about bucket policy syntax, see Policy structure and syntax.

Configure a bucket policy

The following code provides an example on how to configure a bucket policy:

const OSS = require('ali-oss')

const client = new OSS({
  bucket: '<Your BucketName>',
  // This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
  region: '<Your Region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create your RAM user, log on to https://ram.console.aliyun.com.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
});

// Configure the bucket policy.
const policy = {
  Version: '1',
  Statement: [
  {
      Action: ['oss:PutObject', 'oss:GetObject'],
      Effect: 'Deny',
      Principal: ['1234567890'],
      Resource: ['acs:oss:*:1234567890:*/*']
    }
  ]
};

async function putPolicy() {
  const result = await client.putBucketPolicy('<Your Bucket Name>', policy);
  console.log(result)
}

putPolicy()

For more information about how to configure bucket policies, see PutBucketPolicy.

Query a bucket policy

The following code provides an example on how to query the policies configured for a bucket:

const OSS = require('ali-oss')

const client = new OSS({
  bucket: '<Your BucketName>',
  // This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
  region: '<Your Region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create your RAM user, log on to https://ram.console.aliyun.com.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
});

// Query the bucket policy.
async function getPolicy() {
  const result = await client.getBucketPolicy('<Your Bucket Name>');
  console.log(result.policy)
}

getPolicy()

For more information about how to query bucket policies, see GetBucketPolicy.

Delete a bucket policy

The following code provides an example on how to delete the policies configured for a bucket:

const OSS = require('ali-oss')

const client = new OSS({
  bucket: '<Your BucketName>',
  // This example uses the endpoint of the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
  region: '<Your Region>',
  // Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create your RAM user, log on to https://ram.console.aliyun.com.
  accessKeyId: '<Your AccessKeyId>',
  accessKeySecret: '<Your AccessKeySecret>',
});

// Delete the bucket policy.
async function deletePolicy() {
  const result = await client.deleteBucketPolicy('<Your Bucket Name>');
  console.log(result)
}

deletePolicy()

For more information about how to delete bucket policies, see DeleteBucketPolicy.