我需要将ndjson
数据上传到S3存储桶。但我正努力将其传递给S3.putObject()
const responseData = uploadData.data.map((upData) => ({
date: upData.date,
time: upData.time,
url: upData.url,
test1: test1.id,
test2: test2,
}));
JSON
转换为ndjson
:const transformStream = ndjson.stringify();
const outputStream = transformStream.pipe(
fileSystem.createWriteStream(__dirname + '/data.ndjson')
);
responseData.forEach(function iterator(record) {
transformStream.write(record);
});
transformStream.end();
现在,如何将数据上传到我的S3存储桶中,请提供帮助,谢谢
答案 0 :(得分:0)
我能够找到一个整洁的解决方案,而无需ndjson
库
以下代码会将您的JSON
数据转换为ndjson
格式(如果数据属于以下类型):
[
{
"f1": "somedata",
"f2": "somedata",
"f3": "somedata"
},
{
"f1": "somedata",
"f2": "somedata",
"f3": "somedata"
}
]
const transformData = (jsonData) => {
let res = ''
for (let i = 0; i < jsonData.length; i++) {
res += `${JSON.stringify(jsonData[i])}\n`
}
return res
}
然后,您只需调用AWS.S3
-PutObject
函数即可将ndjson
数据放入S3
(如下):
const result = transformData(Data)
const params = {
Bucket: bucketName,
Key: keyName,
Body: result
}
s3.putObject(params, function () {})
// If you need to get the version ID of the Object (only if versioning is Enabled)
// const verID = (await s3.putObject(params, function () {}).promise()).VersionId