node.js - 凭据错误 : Missing credentials in config in nodejs aws-sdk

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

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

Node 代码:

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);
});

最佳答案

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

在 Linux、macOS 或 Unix 上位于 ~/.aws/config,或者在 Windows 上位于 C:\Users\USERNAME.aws\config。

在使用任何 sdk 调用 aws 服务之前,您需要设置此文件,我发布了一个链接,它将指导您如何在不同操作系统中设置 aws cli

Setup AWS CLI

关于node.js - 凭据错误 : Missing credentials in config in nodejs aws-sdk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53611447/

相关文章:

amazon-web-services - 带有区分大小写 header 的 Redshift Unload

node.js - 尝试将 Node js 与 mongoDB Atlas 云连接时出现 UnhandledPromiseRejectionWarning 错误

javascript - Protractor 中可重用方法的回调/ promise

amazon-web-services - 如何在 AWS ECS 服务中监听 "Insufficient cpu/memory"事件?

amazon-web-services - 为 Amazon Aurora 启用数据 API

ruby-on-rails - Rails Paperclip S3 ArgumentError(缺少必需的 :bucket option):

javascript - 如何从 bigquery nodejs api 获取整数?

node.js - 在 Mongoose 中为对象数组创建索引

node.js - 如何从 Node.js 连接到 Amazon DocumentDB

amazon-s3 - Amazon S3 的数据传输和 GET 请求之间的区别