Este tópico descreve como gerenciar as listas de controle de acesso (ACLs) de objetos usando o SDK do Object Storage Service (OSS) para Harmony.
Observações de uso
Para obter mais informações sobre regiões e endpoints, consulte Regiões e endpoints.
Para definir a ACL de um objeto, você deve ter a permissão
oss:PutObjectAcl. Para obter a ACL de um objeto, você deve ter a permissãooss:GetObjectAcl. Para mais informações, consulte Anexar uma política personalizada a um usuário RAM.
Tipos de ACLs
Existem quatro tipos de permissões de acesso a arquivos (ACL):
|
Permissões de acesso |
Descrição |
Valor |
|
Herdada do bucket |
A ACL do objeto é igual à do bucket no qual ele está armazenado. |
default |
|
Privada |
Apenas o proprietário do objeto e os usuários autorizados têm permissões de leitura e gravação no objeto. Outros usuários não podem acessar o objeto. |
private |
|
Leitura pública |
Apenas o proprietário do objeto e os usuários autorizados têm permissões de leitura e gravação no objeto. Outros usuários têm apenas permissão de leitura no objeto. Tenha cuidado ao definir a ACL do objeto com esse valor. |
public-read |
|
Leitura e gravação públicas |
Todos os usuários têm permissões de leitura e gravação no objeto. Tenha cuidado ao definir a ACL do objeto com esse valor. |
public-read-write |
A ACL de um objeto tem precedência sobre a ACL do bucket no qual ele está armazenado. Por exemplo, se a ACL de um objeto em um bucket privado for definida como leitura pública, todos os usuários, incluindo usuários anônimos, podem ler o objeto. Se a ACL de um objeto não estiver configurada, ela será igual à do bucket no qual o objeto está armazenado.
Exemplos
Configure a ACL de um objeto
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();
Consulte as permissões de acesso ao arquivo
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();