All Products
Search
Document Center

Object Storage Service:Manage the ACL of an object

Last Updated:Oct 19, 2023

This topic describes how to manage the access control lists (ACLs) of objects in a versioned bucket.

Configure the ACL of an object

By default, the PutObjectACL operation is called to configure the ACL of the current version of an object. If the current version of the object is a delete marker, Object Storage Service (OSS) returns 404 Not Found. You can specify a version ID in the request to configure the ACL of a specified version of an object.

The following code provides an example on how to configure the ACL of an object:

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 putACL() {
  // Specify the full path of the object. Do not include the bucket name in the full path. 
  const name = 'yourobjectName'
  const acl = 'your acl'
  const versionId = 'your versionId' // Specify the version ID of the object for which you want to configure the ACL. 
 const options = {
    versionId
  };
  const result = await client.putACL(name, acl, options);
  console.log(result);
}

putACL();

Query the ACL of an object

By default, the GetObjectACL operation is called to query the ACL of the current version of an object. If the current version of the object is a delete marker, Object Storage Service (OSS) returns 404 Not Found. You can specify a version ID in the request to query the ACL of the specified version of an object.

The following code provides an example on how to query the ACL of an object:

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 getACL() {
  // Specify the full path of the object. Do not include the bucket name in the full path. 
  const name = 'yourobjectName'
  const versionId = 'your versionId' // Specify the version ID of the object for which you want to query the ACL. 
  const options = {
    versionId
  };
  const result = await client.getACL(name, options);
  console.log(result);
}

getACL();

References

  • For more information about the API operation that you can call to configure the ACL of an object, see PutObjectACL.

  • For more information about the API operation that you can call to query the ACL of an object, see GetObjectACL.