简体   繁体   English

使用 Nodejs 读取存储在 aws S3 中的 JSON 文件

[英]Read JSON file stored in aws S3 with Nodejs

I managed to read a JSON file stored in my S3 bucket but I'm having to do a lot of transformation that I dont fully understand.我设法读取了存储在我的 S3 存储桶中的 JSON 文件,但我不得不做很多我不完全理解的转换。

If we log the data as it comes we get an output of Buffer data如果我们记录数据,我们会得到一个 output 的缓冲区数据

s3.getObject(objParam, (err, data) => {
  if (err) throw err;
  
  console.log("🚀 ~ file: reader.js ~ line 31 ~ rs ~ data", data.Body)

});

// outputs  data <Buffer 7b 0a 20 20 22 74 79 70 65 22 3a 20 22 42 75 66 66 ...

If we convert the above buffer to string with toString('utf-8') I get this:如果我们使用 toString('utf-8') 将上述缓冲区转换为字符串,我会得到:

data.Body.toString('utf8')

// outputs:
{
  "type": "Buffer",
  "data": [
    123,
    34,
    99,
    115,
    112,
    45,
    114,
    101,
    112,
    111,
    114,
....

The only way I managed to see my actual original JSON was converting the above to JSON, accessing the data property, creating a new buffer, then converting back again to toString('utf-8').我设法看到我的实际原始 JSON 的唯一方法是将上面的转换为 JSON,访问数据属性,创建一个新缓冲区,然后再次转换回 toString('utf-8')。

s3.getObject(objParam, (err, data) => {
  if (err) throw err;

  const jsonBytes = JSON.parse(data.Body.toString('utf-8'));
  const buffer = Buffer.from(jsonBytes.data);
  const bufferBackToString = buffer.toString('utf-8');
  console.log("🚀 ~ file: reader.js ~ line 21 ~ s3.getObject ~ bufferBackToString", bufferBackToString);

});

// output: Logs my original JSON!
{
"myProperty": "myData"
} // or whatever...

If it was already a buffer, why did I have to convert to one and convert back again to string?如果它已经是一个缓冲区,为什么我必须转换为一个并再次转换回字符串? Could the buffer have different encoding?缓冲区可以有不同的编码吗?

const jsonBytes = JSON.parse(data.Body.toString('utf-8'));
console.log(jsonBytes);

worked for me, I did see in forums, that s3.getObject returns json directly not string为我工作,我确实在论坛上看到,s3.getObject 直接返回 json 而不是字符串

const getJsonFromS3 = async (filename, bucketName) => {
  const s3Output = await s3.getObject({
    Key: filename,
    Bucket: bucketName
  })

  const jsonString = await s3Output.Body?.transformToString()

  try {
    const json = JSON.parse(jsonString ?? '')
    return json
  } catch (error) {
    console.error('error parsing json', error)
    return null
  }
}

all suggestions with data.toString('utf-8') not working for me. data.toString('utf-8')所有建议都不适合我。 but using transformToString() worked for me但是使用transformToString()对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python,AWS S3:如何使用 jsons 读取文件 - Python, AWS S3: how to read file with jsons AWS Lambda 使用 python 读取 S3 存储桶中的文件 - AWS Lambda read a file in the S3 bucket using python python pandas 从 s3 读取 json gzip 文件 - python pandas read json gzip file from s3 Nodejs上传文件到S3 - Nodejs upload file to S3 function 在从 aws 中的 s3 读取文件后将数据传递给 aws cognito 时退出 lambda 在 nodejs 中 - function exits when passing data to aws cognito after reading file from s3 in aws lambda in nodejs 无法将大文件 (2gb) 放入 aws s3 存储桶 (nodejs) | RangeError:数据太长 - Unable to PUT big file (2gb) to aws s3 bucket (nodejs) | RangeError: data is too long 如何在 NodeJS 中为 AWS S3 对象的不同文件版本生成预签名的 url? - How I can generated pre-signed url for different file versions of AWS S3 objects in NodeJS? 是否可以直接从存储在 S3 上的 zip 文件中读取特定文件? - Is it possible to read a specific file directly from a zip file that is stored on S3? 在将 JSON 文件写入 AWS S3 存储桶后,如何让我的文件另存为 JSON - How do I get my file to be saved as JSON after writing a JSON file into AWS S3 bucket 从 S3 下载文件时 AWS Lambda 中的错误“只读文件系统” - Error "Read-only file system" in AWS Lambda when downloading a file from S3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM