AWS-SDK for NodeJS:如何获取程序中使用的凭证
AWS-SDK for NodeJS: how to get credentials being used in program
我在我的代码中像这样访问 AWS SDK 及其服务:
var aws = require('aws-sdk');
const s3 = new aws.S3();
我想查看初始化 S3 对象时获取的凭据是什么。我尝试了以下方法,显然我无法从文档中找出如何正确使用这些方法和 类。
var credo = aws.config.Credentials().get();
var credo = aws.config.Credentials;
var credo = aws.config.credentials;
var credo = aws.Credentials().get();
var credo = aws.Credentials();
var credo = aws.Credentials;
谁能告诉我获取这些数据的正确方法?我发现这部分的 aws 文档并不容易理解。
编辑:我可以使用 aws.config.update({accessKeyId: 'xxx', secretAccessKey: 'yyy', sessionToken:'zzz'
更新代码中的凭据
当我不这样设置它们时,我想看看这些值是什么。未设置进程环境变量。我已正确设置凭据文件。
您想查看 Nodes 环境变量。
您可以通过process.env
访问所有环境变量
具体来说,你想要这个:
console.log(AWS_SECRET_ACCESS_KEY);
console.log(process.env.AWS_ACCESS_KEY_ID);
您可以从 aws.config.credentials
获取全局配置的凭据
获取accessKeyId:
var accessKeyId = aws.config.credentials.accessKeyId;
获取secretAccessKey:
var secretAccessKey = aws.config.credentials.secretAccessKey;
对于现代 NodeJS,推荐的方法是使用等待:
var AWS = require('aws-sdk');
async function main() {
AWS.config.credentials = new AWS.TemporaryCredentials();
// retrieves credentials according to configuration precedence
// https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html
// updates credentials if expired
await AWS.config.credentials.getPromise()
// after we ensured that credentials are ready we use them
const accessKeyId = AWS.config.credentials.accessKeyId;
const secretAccessKey = AWS.config.credentials.secretAccessKey;
const sessionToken = AWS.config.credentials.sessionToken;
}
我在我的代码中像这样访问 AWS SDK 及其服务:
var aws = require('aws-sdk');
const s3 = new aws.S3();
我想查看初始化 S3 对象时获取的凭据是什么。我尝试了以下方法,显然我无法从文档中找出如何正确使用这些方法和 类。
var credo = aws.config.Credentials().get();
var credo = aws.config.Credentials;
var credo = aws.config.credentials;
var credo = aws.Credentials().get();
var credo = aws.Credentials();
var credo = aws.Credentials;
谁能告诉我获取这些数据的正确方法?我发现这部分的 aws 文档并不容易理解。
编辑:我可以使用 aws.config.update({accessKeyId: 'xxx', secretAccessKey: 'yyy', sessionToken:'zzz'
当我不这样设置它们时,我想看看这些值是什么。未设置进程环境变量。我已正确设置凭据文件。
您想查看 Nodes 环境变量。
您可以通过process.env
具体来说,你想要这个:
console.log(AWS_SECRET_ACCESS_KEY);
console.log(process.env.AWS_ACCESS_KEY_ID);
您可以从 aws.config.credentials
获取accessKeyId:
var accessKeyId = aws.config.credentials.accessKeyId;
获取secretAccessKey:
var secretAccessKey = aws.config.credentials.secretAccessKey;
对于现代 NodeJS,推荐的方法是使用等待:
var AWS = require('aws-sdk');
async function main() {
AWS.config.credentials = new AWS.TemporaryCredentials();
// retrieves credentials according to configuration precedence
// https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html
// updates credentials if expired
await AWS.config.credentials.getPromise()
// after we ensured that credentials are ready we use them
const accessKeyId = AWS.config.credentials.accessKeyId;
const secretAccessKey = AWS.config.credentials.secretAccessKey;
const sessionToken = AWS.config.credentials.sessionToken;
}