问题描述
我有一个按需构建 zip 文件的脚本.使用 Cloud Functions Local Emulator 在本地运行良好.部署后,它不会流式传输响应.构建整个 zip 响应,然后发送.我知道这是因为正在设置 content-length 响应标头,而这并没有在本地发生.
I have a script that is building a zip file on demand. It's working great locally using the Cloud Functions Local Emulator. When deployed, it's not streaming the response. The whole zip response is built, then sent. I know this is happening because the content-length response header is being set, which is not happening locally.
如何让 Cloud Functions 流式传输响应?
How can I get Cloud Functions to stream the response?
exports.zip = (req, res) => {
const zipRequest = {
media: [{
'url': 'https://storage.googleapis.com/...',
'file': 'file1.jpg'
}, {
'url': 'https://storage.googleapis.com/...',
'file': 'file2.jpg'
}, {
'url': 'https://storage.googleapis.com/...',
'file': 'file3.jpg'
}],
filename: 'photos.zip'
};
res.attachment(zipRequest.filename);
res.setHeader('Content-Type', 'application/zip');
let zip = Archiver('zip');
zip.on('end', () => {
res.end();
});
zip.pipe(res);
zipRequest.media.forEach((file) => {
zip.append(request.get(file.url), { name: file.name });
});
zip.finalize();
};
推荐答案
目前没有办法流式传输响应,因为由于基础设施的实现方式,它将缓冲响应,目前没有办法解决,这意味着在响应关闭之前它不会看到结果.
Currently there is no way to stream the response as it will be buffering the response due to how the infrastructure is implemented and there's no way around it right now, which means it would not see the results until the response is closed.
这篇关于云函数响应流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!