Javascript 来自Google Storage的流式文件->;云功能->;存储非常慢或超时
我试图通过我的云功能将一个文件从Google存储桶流式传输到另一个存储桶中的新文件——我的实际用例是转换csv文件中的数据,但为了简单起见,我下面的示例删除了该部分 我有两个桶Javascript 来自Google Storage的流式文件->;云功能->;存储非常慢或超时,javascript,google-cloud-platform,google-cloud-storage,Javascript,Google Cloud Platform,Google Cloud Storage,我试图通过我的云功能将一个文件从Google存储桶流式传输到另一个存储桶中的新文件——我的实际用例是转换csv文件中的数据,但为了简单起见,我下面的示例删除了该部分 我有两个桶和-copy 代码: 我将云功能部署到与bucket相同的区域: gcloud函数部署testStream--运行时节点JS10--欧洲-西部地区2--触发资源--触发事件google.storage.object.finalize--内存=256MB 要触发该函数,我将一个小的100行csv文件复制到src bucket
和-copy
代码:
我将云功能部署到与bucket相同的区域:
gcloud函数部署testStream--运行时节点JS10--欧洲-西部地区2--触发资源
--触发事件google.storage.object.finalize--内存=256MB
要触发该函数,我将一个小的100行csv文件复制到src bucket:
gsutil cp 100Rows.txt gs://
如果我在本地运行该函数,它会像预期的那样立即执行,事实上,正如您可能预期的那样,我可以在线性时间内流式处理1M行文件。然而,上面部署的云功能需要大约45秒来复制这个小文件,而更大的文件似乎永远不会完成。我还注意到管道成功日志位于
函数执行ok日志
2020-04-22 20:20:40.496 BST
testStream1142856940990219Function execution started
2020-04-22 20:20:40.554 BST Processing file: {"bucket":"my-bucket","name":"100Rows.txt"} //removed rest of object for brevity
2020-04-22 20:20:40.650 BST Function execution took 155 ms, finished with status: 'ok'
2020-04-22 20:21:33.841 BST Pipeline succeeded. Time: 53286 ms
有没有关于我哪里出错的想法,或者这是我忽略的已知限制?(我看了很多!)
谢谢
John解决方案分为三个部分:
const util = require('util')
const stream = require('stream')
const pipeline = util.promisify(stream.pipeline);
const {Storage} = require('@google-cloud/storage')
const storage = new Storage()
exports.testStream = (event) => {
const file = event;
console.log(`Processing file: ${JSON.stringify(file)}`)
const startDate = Date.now()
async function run() {
await pipeline(
storage.bucket(file.bucket).file(file.name).createReadStream(),
storage.bucket(file.bucket+'-copy').file(file.name).createWriteStream({gzip: true})
)
console.log('Pipeline complete. Time:', Date.now() - startDate, 'ms')
}
return run().catch(console.error)
}
部署到gcp:
gcloud函数部署testStream——运行时nodejs10——region europe-west2——触发器资源——触发器事件google.storage.object.finalize——内存=2048MB看起来您根本没有处理JavaScript承诺。函数应该返回一个承诺,该承诺在所有异步工作完成时解析。这就是云函数知道何时可以安全终止函数并进行清理的方式。谢谢Doug!最初我确实有一个承诺,但当它仍然不起作用时,我就把它拿了出来……原因是我没有在函数末尾返回run(),因为我盲目地复制了stream.pipeline的节点文档中的示例,它显然在命令行上工作得很好,因为它只是最后一个要执行的语句。我在这件事上损失了几个小时,我觉得有点傻,但这很好,因为它现在起作用了,我非常感谢你的帮助!现在我已经更新了问题中的示例代码。
const util = require('util')
const stream = require('stream')
const pipeline = util.promisify(stream.pipeline);
const {Storage} = require('@google-cloud/storage')
const storage = new Storage()
exports.testStream = (event) => {
const file = event;
console.log(`Processing file: ${JSON.stringify(file)}`)
const startDate = Date.now()
async function run() {
await pipeline(
storage.bucket(file.bucket).file(file.name).createReadStream(),
storage.bucket(file.bucket+'-copy').file(file.name).createWriteStream({gzip: true})
)
console.log('Pipeline complete. Time:', Date.now() - startDate, 'ms')
}
return run().catch(console.error)
}