【问题标题】:Issues Uploading JSON to s3 using node.js使用 node.js 将 JSON 上传到 s3 的问题
【发布时间】:2017-08-12 11:20:34
【问题描述】:

我是 amazon s3 的新手,正在尝试使用 node.js 将 JSON 上传到文件中。我的对象是users,里面有一堆键和值。这是我上传它的方式:

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

但是,当我重新下载它时,它只是一个空对象。

【问题讨论】:

  • users 是 JS 对象,还是实际的 JSON?应该是后者,所以如果是前者,你需要使用JSON.stringify或类似的东西。
  • 我已经尝试使用JSON.stringify,它也不起作用。
  • 请出示相关代码,包括users的示例内容。另外,您检查过putObject 通话的结果吗?您应该为successerror 添加事件处理程序,并记录详细信息。
  • 我添加了事件处理程序,它可以工作。

标签: javascript json node.js amazon-s3


【解决方案1】:

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

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));
  }
);

【讨论】:

  • 我有类似的事情发生,但不断收到Cannot read property 'Location' of undefined
【解决方案2】:

我没有足够的声誉来发表评论,但是对于新的 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
}

【讨论】:

    【解决方案3】:

    JSON.stringify 是在 S3 上创建 JSON 文件的方式。

    AWS 接受序列化的 JSON 字符串作为正文。

    s3.putObject({
        Bucket: 'currenteventstest',
        Key: 'users.json',
        Body: JSON.stringify(users),
        ContentType: 'application/json; charset=utf-8'
    });
    

    【讨论】:

      猜你喜欢
      • 2017-05-22
      • 2017-02-17
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      相关资源
      最近更新 更多