Tous les produits
Search
Centre de documentation

Object Storage Service:Gérer les ACL des objets (SDK Harmony)

Dernière mise à jour :Aug 18, 2026

Cette rubrique explique comment gérer les listes de contrôle d'accès (ACL) des objets à l'aide du SDK Object Storage Service (OSS) pour Harmony.

Remarques sur l'utilisation

Types d'ACL

Il existe quatre types d'autorisations d'accès aux fichiers (ACL) :

Autorisations d'accès

Description

Valeur

Hérité du bucket

L'ACL de l'objet est identique à celle du bucket dans lequel il est stocké.

default

Privé

Seul le propriétaire de l'objet et les utilisateurs autorisés disposent des autorisations de lecture et d'écriture sur l'objet. Les autres utilisateurs ne peuvent pas accéder à l'objet.

private

Lecture publique

Seul le propriétaire de l'objet et les utilisateurs autorisés disposent des autorisations de lecture et d'écriture sur l'objet. Les autres utilisateurs disposent uniquement des autorisations de lecture. Soyez prudent lorsque vous définissez l'ACL de l'objet sur cette valeur.

public-read

Lecture-écriture publique

Tous les utilisateurs disposent des autorisations de lecture et d'écriture sur l'objet. Soyez prudent lorsque vous définissez l'ACL de l'objet sur cette valeur.

public-read-write

L'ACL d'un objet est prioritaire par rapport à l'ACL du bucket dans lequel il est stocké. Par exemple, si l'ACL d'un objet situé dans un bucket privé est définie sur public-read, tous les utilisateurs, y compris les utilisateurs anonymes, peuvent lire l'objet. Si l'ACL d'un objet n'est pas configurée, elle est identique à celle du bucket dans lequel l'objet est stocké.

Exemples

Configurer l'ACL d'un objet

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

// Create an OSSClient instance.
const client = new Client({
  // Specify the AccessKey ID obtained from Security Token Service (STS).
  accessKeyId: 'yourAccessKeyId',
  // Specify the AccessKey secret obtained from STS.
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the security token obtained from STS.
  securityToken: 'yourSecurityToken',
  // 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: 'oss-cn-hangzhou',
});

// Specify the name of the bucket.
const bucket = 'yourBucketName';
// Specify the name of the object for which you want to configure the ACL.
const key = 'yourObjectName';

/**
 * Configure the ACL for the object. 
 * Use the putObjectAcl method to configure the ACL for the object. 
 */
const putObjectAcl = async () => {
  try {
    // Use the putObjectAcl method to configure the ACL for the object.
    const res = await client.putObjectAcl({
      bucket, // Specify the name of the bucket.
      key, // Specify the name of the object.
      acl: EObjectAcl.PRIVATE, //Set the ACL of the object to private.
    });

    // Display the result of the request.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Capture exceptions during the request.
    if (err instanceof RequestError) {
      // If known types of errors exist, display information, such as the error code, error message, request ID, HTTP status code, and EC.
      console.log('code: ', err.code); // The error code.
      console.log('message: ', err.message); // The error message.
      console.log('requestId: ', err.requestId); // The request ID.
      console.log('status: ', err.status); // The HTTP status code.
      console.log('ec: ', err.ec); // The EC.
    } else {
      // Display other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
};

// Call the putObjectAcl function to configure the ACL for the object.
putObjectAcl();

Obtenir les autorisations d'accès aux fichiers

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

// Create an OSSClient instance.
const client = new Client({
  // Specify the AccessKey ID obtained from Security Token Service (STS).
  accessKeyId: 'yourAccessKeyId',
  // Specify the AccessKey secret obtained from STS.
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the security token obtained from STS.
  securityToken: 'yourSecurityToken',
  // 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: 'oss-cn-hangzhou',
});

// Specify the name of the bucket.
const bucket = 'yourBucketName';
// Specify the name of the object whose ACL you want to query.
const key = 'yourObjectName';

/**
 * Query the ACL of the object. 
 * Use the getObjectAcl method to query the ACL of the object. 
 */
const getObjectAcl = async () => {
  try {
    // Use the getObjectAcl method to query the ACL of the object.
    const res = await client.getObjectAcl({
      bucket, // Specify the name of the bucket.
      key, // Specify the name of the object.
    });

    // Display the ACL of the object.
    console.log(JSON.stringify(res));
  } catch (err) {
    // Capture exceptions during the request.
    if (err instanceof RequestError) {
      // If known types of errors exist, display information, such as the error code, error message, request ID, HTTP status code, and EC.
      console.log('code: ', err.code); // The error code.
      console.log('message: ', err.message); // The error message.
      console.log('requestId: ', err.requestId); // The request ID.
      console.log('status: ', err.status); // The HTTP status code.
      console.log('ec: ', err.ec); // The EC.
    } else {
      // Display other unknown types of errors.
      console.log('unknown error: ', err);
    }
  }
};

// Call the getObjectAcl function to query the ACL of the object.
getObjectAcl();