15

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.

Derek Pollard
  • 6,953
  • 6
  • 39
  • 59
Someone
  • 800
  • 1
  • 7
  • 25
  • Is `users` a JS object, or actual JSON? It should be the latter, so if it's the former, you need to use `JSON.stringify` or something equivalent. – jcaron Mar 21 '17 at 00:39
  • I have tried using `JSON.stringify` and it also does not work. – Someone Mar 21 '17 at 00:53
  • Please show the relevant code, including sample content of `users`. Also, have you checked the result of your `putObject` call? You should add event handlers for `success` and `error`, and log details. – jcaron Mar 21 '17 at 00:58
  • I added event handler and it works. – Someone Apr 12 '17 at 19:48

4 Answers4

16

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));
  }
);
jansepke
  • 1,933
  • 2
  • 18
  • 30
Someone
  • 800
  • 1
  • 7
  • 25
8

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
}
Kai Durai
  • 371
  • 3
  • 10
0

JSON.stringify is the way to create a JSON file on S3.

AWS accepts serialized JSON string as Body.

s3.putObject({
    Bucket: 'currenteventstest',
    Key: 'users.json',
    Body: JSON.stringify(users),
    ContentType: 'application/json; charset=utf-8'
});
ifelse.codes
  • 2,289
  • 23
  • 21
0

I had an ES6 Map in my data, which gets stringified to empty by default. I had to refer to below answer to address the issue by manually adding a replacer function to the JSON.Stringify

How do you JSON.stringify an ES6 Map?

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 21 '22 at 08:25