Las políticas de bucket permiten otorgar o restringir el acceso a recursos específicos de Object Storage Service (OSS) para usuarios autenticados, como cuentas de Alibaba Cloud, usuarios de Resource Access Management (RAM) y roles de RAM, o para usuarios anónimos. Por ejemplo, puede otorgar a un usuario de RAM de otra cuenta de Alibaba Cloud acceso de solo lectura a recursos específicos de OSS.
Precauciones
Antes de configurar una política de bucket, familiarícese con esta función. Para obtener más información, consulte Bucket policy.
Para establecer una política de bucket, debe contar con el permiso
oss:PutBucketPolicy. Para obtener una política de bucket, debe contar con el permisooss:GetBucketPolicy. Para eliminar una política de bucket, debe contar con el permisooss:DeleteBucketPolicy. Para obtener más información, consulte Grant custom access policies to a RAM user.
Establecer una política de bucket
A continuación se muestra el código de ejemplo para establecer una 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()
Obtener una política de bucket
A continuación se muestra el código de ejemplo para obtener una 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()
Eliminar una política de bucket
A continuación se muestra el código de ejemplo para eliminar una 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()
Referencias
Para consultar el código de ejemplo completo, véase GitHub.
Para la operación de API que permite establecer una política de bucket, consulte PutBucketPolicy.
Para la operación de API que permite obtener una política de bucket, consulte GetBucketPolicy.
Para la operación de API que permite eliminar una política de bucket, consulte DeleteBucketPolicy.