【问题标题】:Get json data from object from get Storage Amplify从 get Storage Amplify 中获取对象的 json 数据
【发布时间】:2019-11-19 10:29:16
【问题描述】:

我通过 Amplify 从 S3 存储桶中的对象获取了一些数据。

export function amplify() {
  let key = "68a92d44-f25a-4bd8-9543-cc95369ae9a0";
  return Storage.get(key + ".json", {
    download: true
  })
    .then(function(result) {
      return result;
    })
    .catch(err => console.log(err));
}

结果数据如下所示:

Object { LastModified: Date Tue Nov 12 2019, ETag: "\"(lotsofnumbers)\"", ContentType: "application/json", Metadata: {}, Body: Uint8Array(4168) }

我将如何从该对象获取 JSON 数据?

我尝试了 result.Body.toString() ,它为我提供了 JSON 文件及其内容,但我无法编写 result.Body.toString().name 或 .meta(我的 jsonfile 中的内容),例如,它给了我“未定义”。我还尝试使用解析将 Uint8Array 转换为 JSON,而我收到此错误:“JSON.parse:JSON 数据的第 1 行第 1 列的意外字符”。

【问题讨论】:

  • 我已经尝试过了,但它对我不起作用。我再次收到“JSON.parse:JSON 数据的第 1 行第 1 列的意外字符”错误

标签: javascript json reactjs aws-amplify


【解决方案1】:

这对我有用,不知道出了什么问题,但现在可以了 :)

  var obj = amplify();
        obj.then(async function(result) {
          var json = await new Response(result.Body).json();
          console.log(json.meta);
        });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 2016-12-25
    相关资源
    最近更新 更多