program tip

Node.js AWS SDK에서 리전 구성

radiobox 2020. 9. 2. 15:33
반응형

Node.js AWS SDK에서 리전 구성


누군가 Node.js로 누락 된 구성 오류를 수정하는 방법을 설명 할 수 있습니까? aws 문서 페이지 의 모든 예제를 따랐 지만 어쨌든이 오류가 발생합니다.

{ [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 - -

내 코드는 다음과 같습니다.

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'});

(...)

명세서의 순서를 바꾸는 것은 어떻습니까? 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();

"구성에 지역이 없습니다"라는 동일한 문제가 발생했으며 제 경우에는 CLI 또는 Python SDK와 달리 Node SDK가 ~\.aws\config파일 에서 읽지 않습니다 .

이를 해결하기위한 세 가지 옵션이 있습니다.

  1. 프로그래밍 방식으로 구성 (하드 코딩) : AWS.config.update({region:'your-region'});

  2. 환경 변수를 사용하십시오. CLI가를 사용하는 동안 AWS_DEFAULT_REGIONNode SDK는 AWS_REGION.

  3. 다음을 사용하여 JSON 파일에서로드 AWS.config.loadFromPath('./config.json');

JSON 형식 :

{ 
    "accessKeyId": "akid", 
    "secretAccessKey": "secret", 
    "region": "us-east-1" 
}

AWS CLI로 작업하는 경우 ~ / .aws / config에 정의 된 기본 리전이있을 수 있습니다. 불행히도 JavaScript 용 AWS SDK는 기본적으로로드하지 않습니다. 로드하려면 env var를 정의하십시오.

AWS_SDK_LOAD_CONFIG=1

참조 https://github.com/aws/aws-sdk-js/pull/1391를


dynamodb 연결을 생성 할 때 지역을 지정할 수 있습니다 (s3를 시도하지는 않았지만 작동해야 함).

var AWS = require('aws-sdk');
var dd = new AWS.DynamoDB({'region': 'us-east-1'});

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

// 여기에서 다음과 같은 방법으로 AWS 자격 증명을 할당합니다.

AWS.config.update({
  accessKeyId: 'asdjsadkskdskskdk',
  secretAccessKey: 'sdsadsissdiidicdsi',
  region: 'us-east-1'
});

var dd = new AWS.DynamoDB();
var s3 = new AWS.S3();

나에게 같은 오류 :

많은 시련을 겪은 후 아래에 정착했습니다.

  1. 설정된 AWS_REGION전용 로컬 시스템 환경 변수 us-east-1(예)
  2. 이제 지역에 대한 람다 변수를 설정할 필요가 없습니다.
  3. 또한 코드에서 사용할 필요가 없습니다. 예를 들면 다음과 같습니다.

    • AWS.config.update(...), 이것은 필수가 아닙니다.
    • AWS.XYZ(), 이것은 문제없이 작동합니다. XYZ는 S3 또는 기타와 같은 AWS 서비스입니다.

In a rare case if somewhere some defaults are assumed in code and you are forced to send region, then use {'region': process.env.AWS_REGION})


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

To load the AWS config, i do this:

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.


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 -

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';

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

참고URL : https://stackoverflow.com/questions/31039948/configuring-region-in-node-js-aws-sdk

반응형