CredentialsError:Node.js AWS-SDK中的配置中缺少凭据

时间:2018-12-04 11:00:31

标签: node.js amazon-web-services amazon-s3

我正在尝试在aws-s3中上传文件,但是它显示了一些错误,例如 enter image description here

NodeCode:

const AWS = require('aws-sdk');
const uploadFile = async (file) => {

    const s3 = new AWS.S3({
        accessKeyId: "<AWSS3_AccessKey>",
        secretAccessKey: "<AWSS3_SecretKey>",
        region: "ap-south-1"
    });

    const params = {
        Bucket: "test123", // pass your bucket name
        Key: file.name, //filename
        Body: file.data, //data
    };
    s3.upload(params, function(s3Err, data) {
        if (s3Err) throw s3Err
        //console.log(`File uploaded successfully at ${data.Location}`)
    });
};

var files = [];
var fileKeys = Object.keys(req.files);

fileKeys.forEach(function(key) {
    var file = req.files[key];
    files.push(file.name);
    uploadFile(file);
});

1 个答案:

答案 0 :(得分:0)

您的aws cli配置文件未正确配置,配置文件的位置为

在Linux,macOS或Unix上的〜/ .aws / config中,或在Windows上的C:\ Users \ USERNAME.aws \ config中。

在使用任何SDK调用AWS服务之前,您需要先设置此文件,我将发布一个链接,该链接将指导您如何在不同的操作系统中设置AWS CLI

Setup AWS CLI