Node.js 和 aws 凭证错误

Node.js and aws credentials error

我正在尝试开发一个 electron.js 应用程序来将数据上传到 aws s3,但是我在尝试从 json 文件加载我的凭据时遇到了问题。 JSON 文件看起来像这样:

{ "accessKeyId": "my access key", "secretAccessKey": "my secret", "region": "eu-west-1" }

和加载 json 文件的代码:

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

/*
I tried both to load from 'path' string and loading './accounts.json'
*/

var path = process.cwd() + '/accounts.json';
console.log(path);
AWS.config.loadFromPath(path);

var s3 = new AWS.S3();

s3.listBuckets(function (err, data) {
    if (err) {
        console.log(err);
    } 
    else {
        for (var index in data.Buckets) {
            var bucket = data.Buckets[index];
            console.log("Bucket: ", bucket.Name);
        }
    }
});

错误是这样的:

Uncaught TypeError: Cannot read property 'accessKeyId' of null

编辑

我也尝试提供绝对文件夹(例如:'/home/user/project/file.json'),但它不起作用,它只能对凭据进行硬编码,如下所示:

AWS.config.update({
    accessKeyId: "access key id",
    secretAccessKey: "secret access key",
    "region": "eu-west-1"
});

Electron 存在一些无法使其与 aws sdk 一起正常工作的问题

我有一个示例代码,我已经测试过它可以工作。 Link 到 drone.io 输出(以及源代码)。 https://drone.io/github.com/ttwd80/electron-aws/27

我的改变是我没有使用这个:

AWS.config.loadFromPath(path);

我用过这个: 加载 json 文件使其成为一个对象 并使用 AWS.config.update

更新凭据
const keys = require(__dirname + '/keys.json');
AWS.config.update({"accessKeyId": keys.awsAccessKey, "secretAccessKey": keys.awsSecretKey, "region": keys.region});