Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues Uploading JSON to s3 using node.js

I am new to amazon s3 and am trying to use node.js to upload JSON into a file. My object is users, and it has a bunch of keys and values in it. Here is how I'm uploading it:

 s3.putObject({Bucket: 'currenteventstest',Key: 'users.json',Body: users, ContentType: "application/json"});

However, when I re download it, it's just an empty object.

like image 859
Someone Avatar asked Mar 21 '17 00:03

Someone


2 Answers

Adding a callback function fixes the problem:

s3.putObject({
  Bucket: 'currenteventstest',
  Key: 'users.json',
  Body: JSON.stringify(users),
  ContentType: "application/json"},
  function (err,data) {
    console.log(JSON.stringify(err) + " " + JSON.stringify(data));
  }
);
like image 98
Someone Avatar answered Sep 19 '22 11:09

Someone


I don't have enough reputation to comment, but for what its worth with the new aws-sdk version you can use the promise chain when posting the JSON instead of a callback:

try{
   await s3.putObject({
        Bucket: 'currenteventstest',
        Key: 'users.json',
        Body: JSON.stringify(users),
        ContentType: 'application/json; charset=utf-8'
    }).promise();
}
catch(e){
   throw e
}
like image 32
Kai Durai Avatar answered Sep 22 '22 11:09

Kai Durai