All Products
Search
Document Center

Object Storage Service:Manage the ACL of a bucket

Last Updated:Oct 19, 2023

You can configure the access control list (ACL) when you create a bucket, or modify the ACL for a created bucket based on your business requirements. This topic describes how to configure and query the ACL of a bucket.

Bucket ACLs

The following table describes the ACLs that you can configure for a bucket.

ACL

Description

Value

Private

Only the bucket owner and authorized users have read and write permissions on objects in the bucket. Other users cannot access objects in the bucket.

private

Public read

Only the bucket owner and authorized users have read and write permissions on objects in the bucket. Other users have only read permissions on objects in the bucket. Exercise caution when you set the ACL to this value.

public-read

Public read/write

All users have read and write permissions on objects in the bucket. Exercise caution when you set the ACL to this value.

public-read-write

Configure the ACL of a bucket

Configure the ACL when you create a bucket

The following sample code provides an example on how to configure the ACL when you create a bucket:

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 you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'yourbucketname'
});
// In this example, the ACL is set to public read when you create the bucket. 
async function putBucket() {
  const acl = 'public-read';   try {
    await client.putBucket('yourbucketname', { acl });
  } catch (error) {
    console.log(error)
  }
}

putBucket()

Modify the ACL after you create a bucket

The following sample code provides an example on how to modify the ACL after you create a bucket:

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 you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'yourbucketname'
});

async function putBucketACL() {
// In this example, the ACL is changed to private after you create the bucket. 
  const acl = 'private'  
  try {
    await client.putBucketACL('yourbucketname', acl)
  } catch (error) {
    console.log(error)
  }
}

putBucketACL()

Query the ACL of a bucket

The following sample code provides an example on how to query the ACL of a bucket:

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 you have configured environment variables OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET. 
  accessKeyId: process.env.OSS_ACCESS_KEY_ID,
  accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
  // Specify the name of the bucket. 
  bucket: 'yourbucketname'
});

// Query the ACL of the bucket. 
async function getBucketAcl() {
  const result = await client.getBucketACL('yourbucketname')
  console.log('acl: ', result.acl)
}

getBucketAcl()

References

  • For the complete sample code that is used to manage the ACL of a bucket, visit GitHub.

  • For more information about the API operation that you can call to configure the ACL of a bucket, see PutBucketAcl.

  • For more information about the API operation that you can call to query the ACL of a bucket, see GetBucketAcl.