我正在尝试以编程方式上传一个秘密到AWS秘密管理器实例使用"@aws-sdk/client-secrets-manager": "^3.299.0"
,但我总是得到一个错误。我遵循这里的文档:https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-secrets-manager/classes/getsecretvaluecommand.html
我使用的是Typescript版本5.0.2和Node 18。这是我的tsconfig.json:
{
"compilerOptions": {
"module": "NodeNext",
"target": "ESNext",
"noImplicitAny": true,
"preserveConstEnums": true,
"outDir": "./dist",
"sourceMap": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
,这是失败的代码:
const client = new SecretsManagerClient({ region: 'my-region' });
async function getExistingSecret(connectionDetails: ConnectionDetails): Promise<string> {
console.log('checking if secret exists');
const command = getCommand(connectionDetails);
console.log(`GetSecretValueCommand: ${JSON.stringify(command)}`);
return client
.send(command) // <----- FAILS HERE
.catch(() => {
console.log(`secret does not exist`);
return null;
})
.then((response: GetSecretValueCommandOutput) => {
console.log(`secret exists arn: ${response.ARN}`);
return response.ARN;
});
}
const getCommand = (connectionDetails: ConnectionDetails): GetSecretValueCommand => {
const body = getCommandRequest(connectionDetails);
console.log(`GetSecretValueRequest: ${JSON.stringify(body)}`);
return { input: body } as GetSecretValueCommand;
};
const getCommandRequest = (connectionDetails: ConnectionDetails): GetSecretValueCommandInput => {
return { SecretId: secretName(connectionDetails) } as GetSecretValueCommandInput;
};
当我运行这段代码时,我得到的错误是:
TypeError: command.resolveMiddleware is not a function
at SecretsManagerClient.send (/var/task/node_modules/@aws-sdk/smithy-client/dist-cjs/client.js:13:33)
at getExistingSecret (/var/task/aws/secrets-client.js:35:10)
at addSecret (/var/task/aws/secrets-client.js:8:12)
at Runtime.handler (/var/task/index.js:9:29)
at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1085:29)
我已经检查了这些地方以及上面提到的官方文档。
https://github.com/aws/aws-sdk-js-v3/issues/4456
命令。当从EC2 instanceID创建AMI时,resolveMiddleware不是AWS SDK的功能,想知道为什么?
AWS Timestream - SDK V3 Nodejs, TimestreamWriteClient.send() - TypeError:命令。resolvmiddleware不是一个函数。如何解决这个问题?
提前感谢您的帮助!
亚历克斯
我尝试了以下文档,并期望代码不会返回此错误。
非常感谢您的参与;我拼命想弄明白这件事。结果是,GetSecretValueCommand
不应该被实例化为:
const command = { Input: someInput } as GetSecretValueCommand
应该使用提供的构造函数:
const command = new GetSecretValueCommand(someInput)
只有这样对象才会被正确实例化。
再一次,非常感谢你在这方面的投入!请注意:在我看来,同样的"模式"在所有JavaScript V3客户端中都是统一的,所以即使你正在使用Kinesis, S3等,这也会有所帮助。
亚历克斯
这是我在Typescript中使用它的方式,但它也可以在Javascript中使用
函数定义
import { SecretsManagerClient, GetSecretValueCommand, GetSecretValueCommandOutput } from "@aws-sdk/client-secrets-manager";
async function ImportSecrets(secretId?: string): Promise<GetSecretValueCommandOutput> {
const secretManagerClient = new SecretsManagerClient({ region: region });
const secretManagerCommand = new GetSecretValueCommand({ SecretId: secretId });
const secretManagerResponse = await secretManagerClient.send(secretManagerCommand);
return secretManagerResponse
}
函数调用
let secrets = await ImportSecrets('my-secret-name')
注意:-我传递的秘密id名称的功能。当然,在我的情况下,我手动创建秘密。