简体   繁体   English

Heroku S3 在配置中缺少凭据,如果使用 AWS_CONFIG_FILE,请设置 AWS_SDK_LOAD_CONFIG=1',

[英]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.我在通过 S3 将图像上传到 heroku 时遇到问题。 I don't have it locally but when I am in heroku I have this problem.我在本地没有,但是当我在 heroku 时,我遇到了这个问题。 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它在本地工作正常但在 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请帮我解决这些问题

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.如果您在本地运行的代码中通过process.env访问环境变量,那么您还必须为在 Heroku 中运行的应用程序实例定义它们。

On Heroku, they're called "Config Vars", and you can manage them either via the Heroku CLI or through the Dashboard GUI.在 Heroku 上,它们被称为“配置变量”,您可以通过 Heroku CLI 或仪表板 GUI 管理它们。 Here's where you would find it in the GUI, but Heroku's documentation is also worth checking out.在这里您可以在 GUI 中找到它,但Heroku 的文档也值得一试。

Heroku 配置变量

Finally solved the issue for aws in heroku.终于在 heroku 解决了 aws 的问题。

You need to add your aws credential to "config Vars" by going to heroku app settings.您需要转到 heroku 应用程序设置,将您的 aws 凭证添加到“config Vars”。

For example:-例如:- 在此处输入图像描述

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:- (注意:- 在这里我使用 sso 因此我有 aws session 令牌如果不使用 sso 你可能没有它)最后然后添加:-

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这基本上表明 heroku 查找 aws 配置文件,因此在您的根目录或您的 nodejs 应用程序中存在 package.json node_modules 等在这个“.aws”文件夹中创建一个文件夹“.aws”创建或添加包含的“配置”文件

[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.您还可以跳过在 heroku 的配置变量中添加 aws 凭据,只需将此凭据保留在此配置文件中,我尚未对其进行测试。

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.我只是在内部配置文件和 heroku 配置变量中的两个地方都保留了 aws 凭据大声笑已经厌倦了解决错误。

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:不知道它选择了哪一个,但它的工作原理我敢打赌它正在从 heroku 配置变量中选择凭据,因为我正在使用:

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.因此从逻辑上讲,它应该更新从配置文件中获取的凭据。

I got this error because I'd forgotten to set the rails master key on heroku.我收到此错误是因为我忘记在 heroku 上设置 Rails 主密钥。

To do so, simply copy/paste the master key from config/master.key and set it like so:为此,只需从config/master.key中复制/粘贴主密钥并像这样设置它:

heroku config:set RAILS_MASTER_KEY=c9102a13496fdc09430445a6602e3j85

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 配置中缺少凭据,如果使用 AWS_CONFIG_FILE,请设置 AWS_SDK_LOAD_CONFIG=1 - Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1 ChainableTemporaryCredentials getPromise 和 Missing credentials in config,如果使用 AWS_CONFIG_FILE - ChainableTemporaryCredentials getPromise and Missing credentials in config, if using AWS_CONFIG_FILE AWS SDK 配置中缺少凭据 - AWS SDK Missing credentials in config 无法从 ~/.aws/config 和 ~/.aws/credentials 文件加载 AWS 凭证 - Unable to load AWS credentials from ~/.aws/config and ~/.aws/credentials file 使用 aws java sdk 版本 2 从资源文件夹中读取 aws 配置和凭证 - Reading aws config and credentials from resources folder using aws java sdk version 2 Python boto3 如何从 AWS_CONFIG_FILE 解析 role_arn? - Python boto3 how to parse role_arn from AWS_CONFIG_FILE? AWS CLI 从 root 获取凭证/配置 - AWS CLI get credentials/config from root AWS Config 能否写入启用了 object 锁定的 S3 存储桶? - Can AWS Config write to an S3 bucket with object locking enabled? 如何使用 jest mock 模拟 aws-sdk,出现错误“配置中缺少区域” - How to mock aws-sdk using jest mock, getting error "Missing region in config" 如何使用 Scala Spark 在 AWS Glue 作业中设置 Spark Config? - How to set Spark Config in an AWS Glue job, using Scala Spark?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM