O Object Storage Service (OSS) criptografa os dados enviados diretamente no servidor, recurso conhecido como criptografia no lado do servidor. Ao enviar dados para o OSS, o serviço criptografa o conteúdo e o armazena de forma persistente. Durante o download, o OSS descriptografa os dados e retorna o conteúdo original. Além disso, o sistema adiciona um cabeçalho à resposta para indicar que a criptografia ocorreu no servidor.
Configurar a criptografia do bucket
O exemplo de código a seguir demonstra como definir um método de criptografia padrão para um bucket. Após essa configuração, todos os objetos enviados ao bucket sem um método de criptografia específico usam automaticamente o método padrão definido.
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 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,
// Set bucket to the name of your bucket.
bucket: 'yourbucketname'
});
async function putBucketEncryption() {
try {
// Configure the encryption method for the bucket.
const result = await client.putBucketEncryption("bucket-name", {
SSEAlgorithm: "AES256", // This example shows how to set AES256 encryption. If you use KMS encryption, you must add the KMSMasterKeyID property.
// KMSMasterKeyID: "yourKMSMasterKeyId". Set the KMS key ID. You can set this parameter if the encryption method is KMS. If the value of SSEAlgorithm is KMS and you use a specified key for encryption, you must enter the key ID. Otherwise, this parameter must be empty.
});
console.log(result);
} catch (e) {
console.log(e);
}
}
putBucketEncryption();
Obter a configuração de criptografia do bucket
Use o código abaixo para consultar as configurações de criptografia no lado do servidor de um 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 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,
// Set bucket to the name of your bucket.
bucket: 'yourbucketname'
});
async function getBucketEncryption() {
try {
const result = await client.getBucketEncryption("bucket-name");
console.log(result);
} catch (e) {
console.log(e);
}
}
getBucketEncryption();
Excluir a configuração de criptografia do bucket
Para remover as configurações de criptografia no lado do servidor de um bucket, execute o seguinte exemplo:
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 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,
// Set bucket to the name of your bucket.
bucket: 'yourbucketname'
});
async function deleteBucketEncryption() {
try {
// Delete the encryption configuration of the bucket.
const result = await client.deleteBucketEncryption("bucket-name");
console.log(result);
} catch (e) {
console.log(e);
}
}
deleteBucketEncryption();
Referências
Para acessar o código de exemplo completo sobre criptografia no lado do servidor, consulte Exemplos no GitHub.
Para obter mais informações sobre a operação de API usada para definir a criptografia no lado do servidor, consulte PutBucketEncryption.
Para obter mais informações sobre a operação de API usada para recuperar a configuração de criptografia no lado do servidor, consulte GetBucketEncryption.
Para obter mais informações sobre a operação de API usada para excluir a configuração de criptografia no lado do servidor, consulte DeleteBucketEncryption.