As políticas de bucket permitem conceder ou restringir o acesso a recursos específicos do Object Storage Service (OSS) para usuários autenticados, como contas Alibaba Cloud, usuários do Resource Access Management (RAM) e funções RAM, ou para usuários anônimos. Por exemplo, você pode conceder a um usuário RAM de outra conta Alibaba Cloud acesso somente leitura a recursos específicos do OSS.
Precauções
Antes de configurar uma política de bucket, familiarize-se com este recurso. Para mais informações, consulte Bucket policy.
Para definir uma política de bucket, é necessária a permissão
oss:PutBucketPolicy. Para obter uma política de bucket, é necessária a permissãooss:GetBucketPolicy. Para excluir uma política de bucket, é necessária a permissãooss:DeleteBucketPolicy. Para mais informações, consulte Conceder políticas de acesso personalizadas a um usuário RAM.
Definir uma política de bucket
O código abaixo mostra como definir uma política de bucket:
const OSS = require('ali-oss')
const client = new OSS({
// Set region 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 sample 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. For example, examplebucket.
bucket: 'examplebucket'
});
// In this example, the resource owner (bucket owner with UID 174649585760xxxx) uses a bucket policy to grant a RAM user (with UID 20214760404935xxxx) the permissions to list and get objects in the examplebucket bucket.
const policy = {
Version: '1',
Statement: [
{
Action: ['oss:ListObjects', 'oss:GetObject'],
Effect: 'Allow',
Principal: ['20214760404935xxxx'],
Resource: ['acs:oss:*:174649585760xxxx:examplebucket']
}
]
};
async function putPolicy() {
const result = await client.putBucketPolicy('examplebucket', policy);
console.log(result)
}
putPolicy()
Obter uma política de bucket
Veja a seguir o código de exemplo para obter uma política de bucket:
const OSS = require('ali-oss')
const client = new OSS({
// Set region 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 sample 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 bucket policy configuration.
async function getPolicy() {
const result = await client.getBucketPolicy('yourbucketname');
console.log(result.policy)
}
getPolicy()
Excluir uma política de bucket
Confira abaixo o exemplo de código para excluir uma política de bucket:
const OSS = require('ali-oss')
const client = new OSS({
// Set region 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 sample 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'
});
// Delete the bucket policy.
async function deletePolicy() {
const result = await client.deleteBucketPolicy('yourbucketname');
console.log(result)
}
deletePolicy()
Referências
Para obter o código de exemplo completo, consulte o GitHub.
Para consultar a operação de API que define uma política de bucket, veja PutBucketPolicy.
Sobre a operação de API para obter uma política de bucket, consulte GetBucketPolicy.
Para verificar a operação de API que exclui uma política de bucket, acesse DeleteBucketPolicy.