gpt4 book ai didi

node.js - 使用带有 AWS KMS 的 Nodejs 在 s3 中加密和解密文件

转载 作者:搜寻专家 更新时间:2023-10-31 22:42:57 25 4
gpt4 key购买 nike

我正在使用 AWS KMS 将文件加密到 s3 存储桶。我目前正在使用 AWS 控制台执行此操作,但我想使用 Nodejs 执行此操作。

我只是检查了一些东西,但我对使用 nodejs 进行 KMS 的加密和解密没有任何清晰的认识。

最佳答案

您需要查看 AWS SDK for javascript 。来自示例:

var AWS = require('aws-sdk');

var kms = new AWS.KMS({apiVersion: '2014-11-01'});

var params = {
KeyId: "1234abcd-12ab-34cd-56ef-1234567890ab", // The identifier of the CMK to use for encryption. You can use the key ID or Amazon Resource Name (ARN) of the CMK, or the name or ARN of an alias that refers to the CMK.
Plaintext: <Binary String>// The data to encrypt.
};

kms.encrypt(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
/*
data = {
CiphertextBlob: <Binary String>, // The encrypted data (ciphertext).
KeyId: "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"// The ARN of the CMK that was used to encrypt the data.
}
*/
});

var params = {
CiphertextBlob: <Binary String>// The encrypted data (ciphertext).
};

kms.decrypt(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
/*
data = {
KeyId: "arn:aws:kms:us-west-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab", // The Amazon Resource Name (ARN) of the CMK that was used to decrypt the data.
Plaintext: <Binary String>// The decrypted (plaintext) data.
}
*/
});

这是 aws-sdk package on NPM 的链接。这是 main AWS SDK for Javascript documentation page 的链接。

关于node.js - 使用带有 AWS KMS 的 Nodejs 在 s3 中加密和解密文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42107032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com