问题使用node.js将JSON上传到s3

Som*_*one 6 javascript json amazon-s3 node.js

我是亚马逊s3的新手,我正在尝试使用node.js将JSON上传到文件中.我的目标是users,它有一堆键和值.以下是我上传的方式:

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

但是,当我下载它时,它只是一个空对象.

Som*_*one 7

添加回调函数可以解决问题:

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));
  }
);
Run Code Online (Sandbox Code Playgroud)


Kai*_*rai 5

我没有足够的声誉来评论,但是对于新 aws-sdk 版本的价值,您可以在发布 JSON 而不是回调时使用承诺链:

try{
   await s3.putObject({
        Bucket: 'currenteventstest',
        Key: 'users.json',
        Body: JSON.stringify(users),
        ContentType: 'application/json; charset=utf-8'
    }).promise();
}
catch(e){
   throw e
}
Run Code Online (Sandbox Code Playgroud)