All Products
Search
Document Center

Object Storage Service:Manage object ACLs (Harmony SDK)

Last Updated:Mar 20, 2026

Use the OSS SDK for HarmonyOS to set and retrieve the access control list (ACL) of an object. Each object can have its own ACL that overrides the bucket-level ACL, giving you fine-grained control over who can read or write individual objects.

Prerequisites

Before you begin, ensure that you have:

  • An OSS bucket. For region and endpoint information, see Regions and endpoints

  • The RAM permissions required for each operation:

    • To set an object ACL: oss:PutObjectAcl

    • To retrieve an object ACL: oss:GetObjectAcl

    For instructions on granting these permissions, see Attach a custom policy to a RAM user

ACL types

OSS supports four ACL values for objects:

ACLValueWho can readWho can write
Inherited from the bucketdefaultSame as the bucket ACLSame as the bucket ACL
PrivateprivateObject owner and authorized users onlyObject owner and authorized users only
Public-readpublic-readAll users, including anonymous usersObject owner and authorized users only
Public-read-writepublic-read-writeAll users, including anonymous usersAll users, including anonymous users

Object ACLs take precedence over bucket ACLs. For example, if an object in a private bucket has its ACL set to public-read, any user — including anonymous users — can read the object. If an object has no ACL configured, it inherits the bucket's ACL.

Warning

Setting an object ACL to public-read or public-read-write exposes the object to all users on the internet. Grant public access only when your use case explicitly requires it.

Set an object ACL

Use putObjectAcl to set the ACL on an object.

import Client, { EObjectAcl, RequestError } from '@aliyun/oss';

// Initialize the OSS client with STS credentials.
const client = new Client({
  // AccessKey ID obtained from Security Token Service (STS).
  accessKeyId: 'yourAccessKeyId',
  // AccessKey secret obtained from STS.
  accessKeySecret: 'yourAccessKeySecret',
  // Security token obtained from STS.
  securityToken: 'yourSecurityToken',
  // Region where the bucket is located, for example: oss-cn-hangzhou.
  region: 'oss-cn-hangzhou',
});

const bucket = 'yourBucketName';
const key = 'yourObjectName';

const putObjectAcl = async () => {
  try {
    const res = await client.putObjectAcl({
      bucket,
      key,
      acl: EObjectAcl.PRIVATE, // Set to private, public-read, public-read-write, or default.
    });

    console.log(JSON.stringify(res));
  } catch (err) {
    if (err instanceof RequestError) {
      console.log('code: ', err.code);           // Error code.
      console.log('message: ', err.message);     // Error message.
      console.log('requestId: ', err.requestId); // Request ID for troubleshooting.
      console.log('status: ', err.status);       // HTTP status code.
      console.log('ec: ', err.ec);               // EC.
    } else {
      console.log('unknown error: ', err);
    }
  }
};

putObjectAcl();

Get an object ACL

Use getObjectAcl to retrieve the current ACL of an object.

import Client, { RequestError } from '@aliyun/oss';

// Initialize the OSS client with STS credentials.
const client = new Client({
  // AccessKey ID obtained from Security Token Service (STS).
  accessKeyId: 'yourAccessKeyId',
  // AccessKey secret obtained from STS.
  accessKeySecret: 'yourAccessKeySecret',
  // Security token obtained from STS.
  securityToken: 'yourSecurityToken',
  // Region where the bucket is located, for example: oss-cn-hangzhou.
  region: 'oss-cn-hangzhou',
});

const bucket = 'yourBucketName';
const key = 'yourObjectName';

const getObjectAcl = async () => {
  try {
    const res = await client.getObjectAcl({
      bucket,
      key,
    });

    console.log(JSON.stringify(res));
  } catch (err) {
    if (err instanceof RequestError) {
      console.log('code: ', err.code);           // Error code.
      console.log('message: ', err.message);     // Error message.
      console.log('requestId: ', err.requestId); // Request ID for troubleshooting.
      console.log('status: ', err.status);       // HTTP status code.
      console.log('ec: ', err.ec);               // EC.
    } else {
      console.log('unknown error: ', err);
    }
  }
};

getObjectAcl();

Replace the following placeholders with your actual values:

PlaceholderDescriptionExample
yourAccessKeyIdAccessKey ID obtained from STSLTAI5tXxx
yourAccessKeySecretAccessKey secret obtained from STSxXxXxXxXx
yourSecurityTokenSecurity token obtained from STSCAIXxx
yourBucketNameName of the bucketmy-bucket
yourObjectNameName of the objectphotos/image.jpg
oss-cn-hangzhouRegion where the bucket is locatedoss-us-west-1