Defina a lista de controle de acesso (ACL) de um bucket no momento da criação ou modifique-a posteriormente. Este tópico descreve como definir e obter a ACL de um bucket.
Tipos de permissão de leitura e gravação
Um bucket possui três tipos de permissões de acesso:
|
Permissão de acesso |
Descrição |
Valor da ACL |
|
private |
O proprietário do bucket e os usuários autorizados têm permissões de leitura e gravação nos arquivos do bucket. Outros usuários não podem acessar esses arquivos. |
private |
|
public-read |
O proprietário do bucket e os usuários autorizados têm permissões de leitura e gravação nos arquivos do bucket. Outros usuários têm apenas permissão de leitura. Use esta permissão com cautela. |
public-read |
|
public-read-write |
Todos os usuários têm permissões de leitura e gravação nos arquivos do bucket. Use esta permissão com cautela. |
public-read-write |
Definir a ACL de um bucket
Definir a ACL ao criar um bucket
O código a seguir mostra como definir a ACL de um bucket durante a criação:
const OSS = require('ali-oss');
const client = new OSS({
// Set yourregion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourbucketname'
});
// This example shows how to set the ACL of a bucket 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()
Modificar a ACL após criar um bucket
O código a seguir mostra como modificar a ACL de um bucket após a criação:
const OSS = require('ali-oss');
const client = new OSS({
// Set yourregion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourbucketname'
});
async function putBucketACL() {
// This example shows how to modify the ACL of a bucket to private after the bucket is created.
const acl = 'private'
try {
await client.putBucketACL('yourbucketname', acl)
} catch (error) {
console.log(error)
}
}
putBucketACL()
Obter permissões de acesso a um bucket
O código a seguir obtém as permissões de acesso de um bucket:
const OSS = require('ali-oss');
const client = new OSS({
// Set yourregion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to oss-cn-hangzhou.
region: 'yourregion',
// Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
authorizationV4: true,
// Specify the bucket name.
bucket: 'yourbucketname'
});
// Get the ACL of the bucket.
async function getBucketAcl() {
const result = await client.getBucketACL('yourbucketname')
console.log('acl: ', result.acl)
}
getBucketAcl()
Referências
Para acessar o código de exemplo completo de gerenciamento de ACLs de bucket, consulte GitHub examples.
Para obter mais informações sobre a operação da API para definir a ACL de um bucket, consulte PutBucketAcl.
Para obter mais informações sobre a operação da API para recuperar a ACL de um bucket, consulte GetBucketAcl.