'Heroku S3 missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1',

I am facing a problem with uploading an image to heroku via S3. I don't have it locally but when I am in heroku I have this problem. I suspect that thhe credeantials are wrong but it works in localhost. Is there something missing or should I add some code

2020-06-17T16:26:58.525066+00:00 app[web.1]: Error: connect ECONNREFUSED 169.254.169.254:80
2020-06-17T16:26:58.525075+00:00 app[web.1]: at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
2020-06-17T16:26:58.525076+00:00 app[web.1]: message: 'Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1',
2020-06-17T16:26:58.525077+00:00 app[web.1]: errno: 'ECONNREFUSED',
2020-06-17T16:26:58.525077+00:00 app[web.1]: code: 'CredentialsError',
2020-06-17T16:26:58.525078+00:00 app[web.1]: syscall: 'connect',
2020-06-17T16:26:58.525078+00:00 app[web.1]: address: '169.254.169.254',
2020-06-17T16:26:58.525079+00:00 app[web.1]: port: 80,
2020-06-17T16:26:58.525079+00:00 app[web.1]: time: 2020-06-17T16:26:58.517Z,
2020-06-17T16:26:58.525080+00:00 app[web.1]: originalError: {
2020-06-17T16:26:58.525081+00:00 app[web.1]: message: 'Could not load credentials from any providers',

This my code. It works fine locally but not in heroku

const aws = require('aws-sdk');
const fs = require('fs');

aws.config.update({
  secretAccessKey: process.env.AWS_SECRET_KEY,
  accessKeyId: process.env.AWS_ACCESS_KEY,
  region: process.env.AWS_REGION
});


var s3 = new aws.S3({
  secretAccessKey: process.env.AWS_SECRET_KEY,
  accessKeyId: process.env.AWS_ACCESS_KEY,
  region: process.env.AWS_REGION
});

async function addFile(fullpath = String,key = String) {

  const fileContent = fs.readFileSync(fullpath);

  var params = {
    Bucket: 'bucketeer-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    Key: fullpath,
    Body: fileContent

  };

  return new Promise(function (resolve, reject) {
    s3.putObject(params, function (err, data) {
      if (err) {
        console.log(err, err.stack);
        reject(err);
      } // error
      else {
        console.log("added");
        resolve(data);
      }              // deleted
    });
  });


}

module.exports.addFile = addFile;

please help me with this stuff



Solution 1:[1]

You're probably right that the credentials are missing. If you're accessing environment variables via process.env in code that you are running locally, then you will have to define them for the app instance that is running in Heroku as well.

On Heroku, they're called "Config Vars", and you can manage them either via the Heroku CLI or through the Dashboard GUI. Here's where you would find it in the GUI, but Heroku's documentation is also worth checking out.

Heroku Config Vars

Solution 2:[2]

Finally solved the issue for aws in heroku.

You need to add your aws credential to "config Vars" by going to heroku app settings.

For example:- enter image description here

aws_access_key_id=<add aws_access_key_id here>
aws_secret_access_key=<add aws_secret_access_key here>
aws_session_token=<add aws_session_token here>

(note:- here i am using sso hence i have aws session token if not using sso you might not have it) finally then add:-

AWS_SDK_LOAD_CONFIG=1     <- this is super important 

This basically indicates heroku to look for aws config file hence in your root directory or in your nodejs app where package.json node_modules etc are present create a folder ".aws" inside this ".aws" folder create or add "config" file containing

[default]
region = us-east-1
output = json

You can also skip adding aws credential in config vars of heroku and just keep this credential inside this config file i have not tested it yet.

I just kept aws credential at both places in inside config file as well as inside heroku config vars lol was getting tired of solving errors.

Don't know which one it's picking up but its working i bet it's picking credentials from heroku config vars as i am using:

AWS.config.update({
  region: "us-east-1",
  aws_access_key_id: process.env.aws_access_key_id,
  aws_secret_access_key: process.env.aws_secret_access_key,
  aws_session_token:process.env.aws_session_token
});

Hence logically it should update the credential taken from config file.

Solution 3:[3]

I got this error because I'd forgotten to set the rails master key on heroku.

To do so, simply copy/paste the master key from config/master.key and set it like so:

heroku config:set RAILS_MASTER_KEY=c9102a13496fdc09430445a6602e3j85

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 IlyaMeer
Solution 2 Henry Ecker
Solution 3 stevec