Javascript 在 Node.js AWS SDK 中配置区域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31039948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configuring region in Node.js AWS SDK
提问by Anejah Daniels
Can someone explain how to fix a missing config error with Node.js? I've followed all the examples from the aws doc pagebut I still get this error no matter what.
有人可以解释如何使用 Node.js 修复丢失的配置错误吗?我已经遵循了aws doc 页面中的所有示例,但无论如何我仍然收到此错误。
{ [ConfigError: Missing region in config]
message: 'Missing region in config',
code: 'ConfigError',
time: Wed Jun 24 2015 21:39:58 GMT-0400 (EDT) }>{ thumbnail:
{ fieldname: 'thumbnail',
originalname: 'testDoc.pdf',
name: 'testDoc.pdf',
encoding: '7bit',
mimetype: 'application/pdf',
path: 'uploads/testDoc.pdf',
extension: 'pdf',
size: 24,
truncated: false,
buffer: null } }
POST / 200 81.530 ms - -
Here is my code:
这是我的代码:
var express = require('express');
var router = express.Router();
var AWS = require('aws-sdk');
var dd = new AWS.DynamoDB();
var s3 = new AWS.S3();
var bucketName = 'my-bucket';
AWS.config.update({region:'us-east-1'});
(...)
回答by umbrel
How about changing the order of statements? Update AWS config before instantiating s3 and dd
如何改变语句的顺序?在实例化 s3 和 dd 之前更新 AWS 配置
var AWS = require('aws-sdk');
AWS.config.update({region:'us-east-1'});
var dd = new AWS.DynamoDB();
var s3 = new AWS.S3();
回答by Zanon
I had the same issue "Missing region in config" and in my case it was that, unlike in the CLI or Python SDK, the Node SDK won't read from the ~\.aws\config
file.
我有同样的问题“配置中缺少区域”,在我的情况下,与 CLI 或 Python SDK 不同,Node SDK 不会从~\.aws\config
文件中读取。
To solve this, you have three options:
要解决这个问题,您有三个选择:
Configure it programmatically (hard-coded):
AWS.config.update({region:'your-region'});
Use an environment variable. While the CLI uses
AWS_DEFAULT_REGION
, the Node SDK usesAWS_REGION
.Load from a JSON file using
AWS.config.loadFromPath('./config.json');
以编程方式配置(硬编码):
AWS.config.update({region:'your-region'});
使用环境变量。当 CLI 使用 时
AWS_DEFAULT_REGION
,Node SDK 使用AWS_REGION
.使用从 JSON 文件加载
AWS.config.loadFromPath('./config.json');
JSON format:
JSON 格式:
{
"accessKeyId": "akid",
"secretAccessKey": "secret",
"region": "us-east-1"
}
回答by Peter Dotchev
If you work with AWS CLI, you probably have a default region defined in ~/.aws/config. Unfortunately AWS SDK for JavaScript does not load it by default. To load it define env var
如果您使用 AWS CLI,您可能在 ~/.aws/config 中定义了一个默认区域。不幸的是,AWS SDK for JavaScript 默认不会加载它。要加载它定义 env var
AWS_SDK_LOAD_CONFIG=1
回答by WaffleSouffle
You can specify the region when creating the dynamodb connection (haven't tried s3 but that should work too).
您可以在创建 dynamodb 连接时指定区域(还没有尝试过 s3,但应该也可以)。
var AWS = require('aws-sdk');
var dd = new AWS.DynamoDB({'region': 'us-east-1'});
回答by BHUVNESH KUMAR
var AWS = require('aws-sdk');
// assign AWS credentials here in following way:
// 通过以下方式在此处分配 AWS 凭证:
AWS.config.update({
accessKeyId: 'asdjsadkskdskskdk',
secretAccessKey: 'sdsadsissdiidicdsi',
region: 'us-east-1'
});
var dd = new AWS.DynamoDB();
var s3 = new AWS.S3();
回答by Himanshu Tongya
I have gone through your code and here you are connecting to AWS services before setting the region, so i suggest you to update the region first and then connect to services or create instance of those as below -
我已经完成了您的代码,在这里您在设置区域之前连接到 AWS 服务,因此我建议您先更新区域,然后连接到服务或创建以下服务的实例 -
var express = require('express');
var router = express.Router();
var AWS = require('aws-sdk');
AWS.config.update({region:'us-east-1'});
var dd = new AWS.DynamoDB();
var s3 = new AWS.S3();
var bucketName = 'my-bucket';
回答by Manohar Reddy Poreddy
Same error for me:
对我来说同样的错误:
After doing a lot of trials I have settled on the below:
经过大量试验,我确定了以下内容:
OPTION 1
选项1
- set the
AWS_REGION
environment variable in local system only, tous-east-1
(example)
AWS_REGION
仅在本地系统中设置环境变量,以us-east-1
(示例)
For Linux:
对于 Linux:
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
export AWS_DEFAULT_REGION=us-east-1
导出 AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
导出 AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
导出 AWS_DEFAULT_REGION=us-east-1
For Windows
see: https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html
对于 Windows,
请参阅:https: //docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html
- now, no need to set any lambda variables for region
also, no need to use in code, for example:
AWS.config.update(...)
, this is notrequiredAWS.S3()
, etc., these will work without any problems. In place of S3, there can be any aws service
- 现在,无需为区域设置任何 lambda 变量
此外,无需在代码中使用,例如:
AWS.config.update(...)
,这不是必需的AWS.S3()
等,这些都可以正常工作。代替 S3,可以有任何 aws 服务
In a rare caseif somewhere some defaults are assumed in code and you are forced to send region, then use {'region': process.env.AWS_REGION})
在极少数情况下,如果代码中假定了某些默认值并且您被迫发送区域,则使用{'region': process.env.AWS_REGION})
OPTION 2
选项 2
Instead of environment variables, another way is AWS CONFIG file:
而不是环境变量,另一种方法是 AWS CONFIG 文件:
On Linux you can create below files:
在 Linux 上,您可以创建以下文件:
~/.aws/credentials
~/.aws/凭证
[default]
aws_access_key_id=AKIAIOSFODNN7EXAMPLE
aws_secret_access_key=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
~/.aws/config
~/.aws/config
[default]
region=us-west-2
output=json
See https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html
请参阅https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html
回答by Tony
This may not be the right way to do it, but I have all my configs in a separate JSON file. And this does fix the issue for me
这可能不是正确的方法,但我将所有配置都放在一个单独的 JSON 文件中。这确实为我解决了这个问题
To load the AWS config, i do this:
要加载 AWS 配置,我这样做:
var awsConfig = config.aws;
AWS.config.region = awsConfig.region;
AWS.config.credentials = {
accessKeyId: awsConfig.accessKeyId,
secretAccessKey: awsConfig.secretAccessKey
}
config.aws is just a JSON file.
config.aws 只是一个 JSON 文件。
回答by Sajeetharan
You could create a common module and use it based on the region you want to
您可以创建一个通用模块并根据您想要的区域使用它
var AWS = require('aws-sdk')
module.exports = {
getClient: function(region) {
AWS.config.update({ region: region })
return new AWS.S3()
}
}
and consume it as,
并将其消耗为,
var s3Client = s3.getClient(config.region)
the idea is to Update AWS config before instantiating s3
这个想法是在实例化 s3 之前更新 AWS 配置