当我尝试向我的 S3 存储桶 (Node.js) 发送内容时,AWS 缺少凭证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26284181/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:58:35  来源:igfitidea点击:

AWS Missing credentials when i try send something to my S3 Bucket (Node.js)

node.jsamazon-web-servicesamazon-s3

提问by Hugh McKee

Hei!

嘿!

I'm having this issue since yesterday, and I'm having trouble for find a solution.

我从昨天开始就有这个问题,我很难找到解决方案。

I'm trying to send somethings to my S3 bucket, but this message appear in my console when i try:

我正在尝试向我的 S3 存储桶发送一些内容,但是当我尝试时,此消息出现在我的控制台中:

{ [CredentialsError: Missing credentials in config]
  message: 'Missing credentials in config',
  code: 'CredentialsError',
  errno: 'Unknown system errno 64',
  syscall: 'connect',
  time: Thu Oct 09 2014 14:03:56 GMT-0300 (BRT),
  originalError: 
   { message: 'Could not load credentials from any providers',
     code: 'CredentialsError',
     errno: 'Unknown system errno 64',
     syscall: 'connect',
     time: Thu Oct 09 2014 14:03:56 GMT-0300 (BRT),
     originalError: 
      { code: 'Unknown system errno 64',
        errno: 'Unknown system errno 64',
        syscall: 'connect',
        message: 'connect Unknown system errno 64' } } }

And this is my code:

这是我的代码:

var s3 = new AWS.S3();
AWS.config.loadFromPath('./AwsConfig.json'); 

    s3.putObject(params, function(err) {
        if(err) {
            console.log(err);
        }
        else {
            console.log("Succes");
        }
});

The credentials are correct. Does anyone know what can be? I've searching but I not find anywhere the solution.

凭据是正确的。有谁知道可以是什么?我一直在搜索,但找不到任何解决方案。



My credentials(fake):

我的凭据(假):

{
    "accessKeyId": "BLALBLALBLALLBLALB",
    "secretAccessKey": "BLABLALBLALBLALBLLALBLALLBLALB",
    "region": "sa-east-1",
    "apiVersions": {
      "s3": "2006-03-01",
      "ses": "2010-12-01"
    }
}


EDIT:

编辑:

For help, all the code:

如需帮助,所有代码:

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


var s3 = new AWS.S3();
AWS.config.loadFromPath('./MYPATH.json'); //this is my path to the aws credentials.


var params = {
        Bucket: 'testing-dev-2222',
        Key: file,
        Body: fs.createReadStream(file)
    };

s3.putObject(params, function(err) {
    if(err) {
        console.log(err);
    }
    else {
        console.log("Success");
    }
});


New err:

新错误:

Error uploading data:  { [PermanentRedirect: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.]
  message: 'The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.',
  code: 'PermanentRedirect',
  time: Thu Oct 09 2014 14:50:02 GMT-0300 (BRT),
  statusCode: 301,
  retryable: false }

回答by xShirase

Try hardcoding your params and see if you get the error again :

尝试对您的参数进行硬编码,看看是否再次出现错误:

AWS.config.update({
    accessKeyId: "YOURKEY",
    secretAccessKey: "YOURSECRET",
    "region": "sa-east-1"   <- If you want send something to your bucket, you need take off this settings, because the S3 are global. 
}); // for simplicity. In prod, use loadConfigFromFile, or env variables

var s3 = new AWS.S3();
var params = {
    Bucket: 'makersquest',
    Key: 'mykey.txt',
    Body: "HelloWorld"
};
s3.putObject(params, function (err, res) {
    if (perr) {
        console.log("Error uploading data: ", err);
    } else {
        console.log("Successfully uploaded data to myBucket/myKey");
    }
});

Good resource here

好资源在这里

回答by Hugh McKee

I had the same problem until I reversed the two lines:

我遇到了同样的问题,直到我颠倒了两行:

var s3 = new AWS.S3();
AWS.config.loadFromPath('./AwsConfig.json'); 

to this:

对此:

AWS.config.loadFromPath('./AwsConfig.json'); 
var s3 = new AWS.S3();

回答by Dipankar Biswas

I was having the same error. But I found the issue. I was using wrong Environment variable name. From NodeJS to S3, I need to use the following variable names:

我有同样的错误。但是我发现了这个问题。我使用了错误的环境变量名称。从 NodeJS 到 S3,我需要使用以下变量名:

process.env.AWS_ACCESS_KEY_ID = 'XXXXXXXXXXXXXXXXXXX';
process.env.AWS_SECRET_ACCESS_KEY = 'XXXXXXXXXXXXXXXXXXXXXXXXXX';
process.env.AWS_REGION = 'us-east-1';

Once I corrected the variable names, it just ran fine. regards, Dipankar

一旦我更正了变量名称,它就运行良好。问候, 迪潘卡尔

回答by timxor

Try changing the user in my aws config file from a specific user to [default].

尝试将我的 aws 配置文件中的用户从特定用户更改为 [default]。

$nano .aws/credentials

[default]
aws_access_key_id = xyz
aws_secret_access_key = xyz

If you do not have this file, create it and get your keys or generate new one from aws iam user keys.

如果您没有此文件,请创建它并获取您的密钥或从 aws iam 用户密钥生成新的密钥。

回答by Gopal Shukla

I tried above option and even that did not work, so I created new config object and this below code worked

我尝试了上面的选项,即使它也不起作用,所以我创建了新的配置对象,下面的代码有效

 AWS.config = new AWS.Config();
 AWS.config.accessKeyId = "AccessKey";
 AWS.config.secretAccessKey = "SecretAccessKey";

回答by Dustin Graves

I had a similar issue when trying to load the config file from anywhere other than the root directory.

尝试从根目录以外的任何地方加载配置文件时,我遇到了类似的问题。

I was able to load the json file natively in node, then just pass the object that was parsed to AWS.config.update()

我能够在节点中本地加载 json 文件,然后只需将解析的对象传递给 AWS.config.update()

import AWS from 'aws-sdk'
import config from '../aws.json' 
AWS.config.update(config);

回答by SUDARSHAN

This resolved my issue .

这解决了我的问题。

  1. Used the sample code from the Cognito Console and added it to the of the document.

  2. Enabled Unauthenticated access on the identity pool.

  1. 使用来自 Cognito 控制台的示例代码并将其添加到文档的 。

  2. 在身份池上启用未经身份验证的访问。

Most important

最重要的

  1. Fixed the Trust Relationship policy in the unauth role so the Cognito Service could assume the role.

  2. Do not hard code credential in the file .

  1. 修复了 unauth 角色中的信任关系策略,以便 Cognito 服务可以承担该角色。

  2. 不要在文件中硬编码凭证。