O Object Storage Service (OSS) pode criptografar dados enviados no lado do servidor. Esse processo é chamado de criptografia no lado do servidor. Ao fazer upload de dados para o OSS, o serviço criptografa os dados enviados e os armazena de forma persistente já criptografados. Ao baixar dados do OSS, o serviço descriptografa e retorna o conteúdo original. Além disso, um cabeçalho é incluído na resposta para indicar que os dados foram criptografados no servidor.
Configurar a criptografia do bucket
O código de exemplo a seguir demonstra como configurar um método de criptografia padrão para um bucket. Após definir o método de criptografia padrão, todos os objetos enviados ao bucket sem um método de criptografia especificado serão criptografados com o método padrão.
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
O código de exemplo a seguir mostra como 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
O código de exemplo a seguir ilustra como excluir 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 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 de criptografia no lado do servidor, consulte GitHub examples.
Para mais informações sobre a operação de API usada para definir a criptografia no lado do servidor, consulte PutBucketEncryption.
Para conhecer a operação de API usada para recuperar a configuração de criptografia no lado do servidor, consulte GetBucketEncryption.
Para detalhes sobre a operação de API usada para excluir a configuração de criptografia no lado do servidor, consulte DeleteBucketEncryption.