node.js - 如何在 AWS js SDK 中配置区域?

标签 node.js amazon-web-services aws-sdk aws-sdk-js aws-regions

我的问题

我正在编写一个简单的 js 函数,用于从 AWS CloudWatch Logs 读取一些信息。

按照 Configuring region in Node.js AWS SDK 的回答, 和 AWS nodejs SDK documentation ,我想出了以下内容:

代码

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

var cloudwatchlogs = new AWS.CloudWatchLogs();

console.log(AWS.config.region)              // Undefined

AWS.config.region = 'eu-central-1'          // Define the region with dot notation
console.log(AWS.config.region) .            // eu-central-1

AWS.config.update({region:'eu-central-1'}); // Another way to update
console.log(AWS.config.region) .            // eu-central-1


var params = {
  limit: 0,
//   logGroupNamePrefix: 'STRING_VALUE',
//   nextToken: 'STRING_VALUE'
};

// This call is failing
cloudwatchlogs.describeLogGroups(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

输出和错误

undefined
eu-central-1
eu-central-1
{ ConfigError: Missing region in config
    at Request.VALIDATE_REGION (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/event_listeners.js:91:45)
    at Request.callListeners (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
    at callNextListener (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/sequential_executor.js:95:12)
    at /Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/event_listeners.js:85:9
    at finish (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/config.js:315:7)
    at /Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/config.js:333:9
    at SharedIniFileCredentials.get (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/credentials.js:126:7)
    at getAsyncCredentials (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/config.js:327:24)
    at Config.getCredentials (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/config.js:347:9)
    at Request.VALIDATE_CREDENTIALS (/Users/adam/binaris/adam-test-sls/node_modules/aws-sdk/lib/event_listeners.js:80:26)
  message: 'Missing region in config',
  code: 'ConfigError',
  time: 2017-07-11T09:57:55.638Z } ...

环境

代码在 node v8.1.2 下本地运行。

我的问题

如何在 AWS js SDK 中正确配置区域?

附录

已打开 an issue on github并得到了一些回应。

最佳答案

或者,您可以在创建 cloudwatch 对象时指定:

var AWS = require('aws-sdk');
var cloudwatchlogs = new AWS.CloudWatchLogs({region: 'eu-central-1'});

关于node.js - 如何在 AWS js SDK 中配置区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45032362/

相关文章:

amazon-web-services - Spring Cloud SQS - 轮询间隔

javascript - forEach 循环内的异步 findOne() 操作

node.js - 为什么 POST 重定向到 GET 而 PUT 重定向到 PUT?

javascript - 如何检查对象中的 hasOwnProperty 链

node.js - 如何使用 Express 和 Node 仅在 POST 上添加中间件

amazon-web-services - 用于具有多个 CIDR IP 的云形成安全组的 YAML CF 模板

amazon-web-services - AWS Appsync 架构错误(资源未处于状态 stackUpdateComplete)

amazon-web-services - 如何使用 AWS CloudFormation 为 SNS 订阅指定 'Raw Message Delivery'?

javascript - 使用 aws-sdk 时如何在 AWS Lambda 中创建异步/等待函数

amazon-s3 - AmazonS3Client 中的 putObject 是否阻塞?