'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.
Solution 1:[1]
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
}
Solution 2:[2]
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'
});
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | ifelse.codes |
| Solution 3 | John Smith |
