这是我的函数,用于获取多个对象的内容并合并到一个文件中:
const getAllObjectsInRegions = (config, keys) => {
keys.forEach((key) => {
const params = {
Bucket: `${config.metadata.s3.bucket}`,
Key: key
}
const file = fs.createWriteStream('./out.txt')
return s3.getObject(params)
.createReadStream()
.pipe(file)
})
基本上我读的对象只是一个json文件。您是否知道如何在写入文件之前转换对象的内容?我需要提取一些属性来编写整个文件。谢谢
编辑:
const getAllObjectsInRegions = (config, keys) => {
const promise = new Promise((resolve, reject) => {
keys.forEach((key) => {
const params = {
Bucket: `${config.metadata.s3.bucket}`,
Key: key
}
const readStream = s3.getObject(params).createReadStream()
readStream.on('data', (data) => {
let playlist
try {
playlist = JSON.parse(data)
} catch(err) {
reject(err)
}
const slicedPlaylist = _.pick(playlist, ['contentKey', 'title', 'text' ])
fs.writeFile('./out.txt', slicedPlaylist, (err) => {
if (err) {
reject(err)
}
resolve()
})
})
})
})
return promise
}