Umberto Palazzini
Umberto Palazzini

Reputation: 86

Node.js and aws credentials error

i'm trying to develop an electron.js app to upload data to aws s3, but i have a problem while trying to load my credentials from a json file. The JSON file looks like that:

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

and the code that load the json file:

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

The error is this:

Uncaught TypeError: Cannot read property 'accessKeyId' of null

EDIT

I also tried providing absolute folder (e.g.: '/home/user/project/file.json'), but it doesn't work, it only works hard coding the credentials, like this:

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

Upvotes: 0

Views: 4353

Answers (2)

Titi Wangsa bin Damhore
Titi Wangsa bin Damhore

Reputation: 7199

I have a sample code that I have tested and it works. Link to drone.io output (as well as the source code). https://drone.io/github.com/ttwd80/electron-aws/27

My change was that I did not use this:

AWS.config.loadFromPath(path);

I used this: load the json file to make it an object and update the credentials using AWS.config.update

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

Upvotes: 3

Umberto Palazzini
Umberto Palazzini

Reputation: 86

Electron has some issues that doesn't make it work properly with aws sdk

Upvotes: -1

Related Questions