Configuring region in Node.js AWS SDK

前端 未结 14 1521
失恋的感觉
失恋的感觉 2020-12-05 03:30

Can someone explain how to fix a missing config error with Node.js? I\'ve followed all the examples from the aws doc page but I still get this error no matter what.

相关标签:
14条回答
  • 2020-12-05 04:17

    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';
    
    0 讨论(0)
  • 2020-12-05 04:18

    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_SDK_LOAD_CONFIG=1
    

    See https://github.com/aws/aws-sdk-js/pull/1391

    0 讨论(0)
  • 2020-12-05 04:20

    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.

    0 讨论(0)
  • 2020-12-05 04:24

    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

    0 讨论(0)
  • 2020-12-05 04:24

    I'm impressed this hasn't been posted here yet.

    Instead of setting the region with AWS.config.update(), you can use

    const s3 = new AWS.S3({
      region: "eu-central-1",
    });
    

    to make it instance specific.

    0 讨论(0)
  • 2020-12-05 04:24

    You can resolve this issue right in your project directory.

    1. npm i -D dotenv.
    2. Create .env file in root of our project.
    3. Set environment variable AWS_SDK_LOAD_CONFIG=1 in that .env file.
    4. const {config} = require("dotenv"); in the same file where you configure connection to DynamoDB.
    5. config() before you new AWS.DynamoDB().

    P.S. As someone have mentioned before, problem is that Node doesn't get data from your aws.config file

    0 讨论(0)
提交回复
热议问题