Issues Uploading JSON to s3 using node.js

前端 未结 3 1634
轻奢々
轻奢々 2021-01-12 22:05

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

相关标签:
3条回答
  • 2021-01-12 22:13

    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
    }
    
    0 讨论(0)
  • 2021-01-12 22:18

    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'
    });
    
    0 讨论(0)
  • 2021-01-12 22:33

    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));
      }
    );
    
    0 讨论(0)
提交回复
热议问题