gpt4 book ai didi

node.js - "EC2 Metadata roleName request returned error"使用带有 NestJS 的 aws 加密库

转载 作者:行者123 更新时间:2023-12-04 17:27:40 29 4
gpt4 key购买 nike

我正在尝试在 NestJS 应用程序中使用 aws 加密 sdk,下一个代码源显示了实现。

import { Controller, Get } from '@nestjs/common';
import {KmsKeyringNode, encrypt} from '@aws-crypto/client-node'


@Controller('encryption')
export class EncryptionController {
// constructor() {}
@Get()
async crypt() {
const generatorKeyId = "generatior key";
const masterKeyId = "master key id";
const keyring = new KmsKeyringNode({keyIds:[masterKeyId], generatorKeyId: generatorKeyId});
const plainText = "My passwords for senstive data";
const context = {
accountId: "100",
purpose: "youtube demo",
country: "Sri Lanka"
};
const { result } = await encrypt(keyring, plainText, { encryptionContext: context });
console.log(result)


}

}

但是当我执行请求以查看我的实现结果时,我收到下一个错误
Error: connect EHOSTUNREACH 169.254.169.254:80
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14) {
message: 'Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1',
errno: 'EHOSTUNREACH',
code: 'CredentialsError',
syscall: 'connect',
address: '169.254.169.254',
port: 80,
time: 2020-06-09T11:41:47.638Z,
originalError: {
message: 'Could not load credentials from any providers',
errno: 'EHOSTUNREACH',
code: 'CredentialsError',
syscall: 'connect',
address: '169.254.169.254',
port: 80,
time: 2020-06-09T11:41:47.638Z,
originalError: {
message: 'EC2 Metadata roleName request returned error',
errno: 'EHOSTUNREACH',
code: 'EHOSTUNREACH',
syscall: 'connect',
address: '169.254.169.254',
port: 80,
time: 2020-06-09T11:41:47.637Z,
originalError: [Object]
}
}
}

但是我从这个 tutorial 复制了一个 nodejs 脚本看看它是否适用于我的 aws sdk 设置
而且我没有收到任何错误,即使在 cli 中执行 aws kms 加密方法我也没有任何错误。

我试图导出 AWS_SDK_LOAD_CONFIG=1可变我在其他类似错误中的看法。

有谁知道发生了什么?

最佳答案

我有同样的问题。这让我很头疼,因为我在 AWS Fargate 中运行了它,并且在那里调试并不容易。
该错误意味着 Javascript SDK 找不到 AWS 凭证。
在这里,您可以看到 SDK 尝试从以下位置加载凭据的顺序:
https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html
我的错误很尴尬,我只是在我的环境变量中有一个错字。我的变量是 AWS_ACCESSS_KEY_ID而不是 AWS_ACCESS_KEY_ID . (很难看出区别,对吧?)
所以可能要仔细检查你的环境变量(或配置文件)的名称

关于node.js - "EC2 Metadata roleName request returned error"使用带有 NestJS 的 aws 加密库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62282013/

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