【问题标题】:Download file from /tmp to client in Google Cloud Function将文件从 /tmp 下载到 Google Cloud Function 中的客户端
【发布时间】:2021-11-07 06:38:01
【问题描述】:

我正在尝试下载在 /tmp 目录中保存的 http Google Cloud 函数上创建的文件。我尝试的一切都会抛出“错误:无法处理请求”。

我的代码生成的文件保存在/tmp/output.wav,我可以使用fs.readdirSync('/tmp') 查看该文件。但是,如果我尝试res.download('/tmp/output.wav', 'output.wav'),它会抛出“错误:无法处理请求”。

如果我这样做res.send( fs.readdirSync('/tmp')[0] );,我可以看到该文件。所以我知道它在那里并且可读。为什么我不能下载到客户端?

我检查了日志,没有任何其他内容。这段代码也在我的机器上本地执行。

我想知道这是否是由于配额限制?如果是这样,我会得到更好的错误吗?

完整代码:

exports.master = async (req, res ) => {
    const fs = require('fs');

    // Some code to make the file at /tmp/output.wav

    fs.readdirSync('/tmp').forEach(file => {
        console.log(file);
        files.push( file );
    });
    // Code will fire up to this point
    res.download('/tmp/output.wav', 'output.wav');
});

【问题讨论】:

  • 请编辑问题以显示重现问题的minimal complete code。我们应该能够使用您显示的内容复制问题。您可能在我们看不到的地方犯了错误。
  • 什么时候在/tmp中创建文件?

标签: node.js google-cloud-platform google-cloud-functions


【解决方案1】:

你的代码在我这边工作:

exports.master = async (req, res) => {
    const fs = require('fs')

    // Some code to make the file at /tmp directory
    fs.writeFileSync('/tmp/output.txt','testing output')
    var files = [{}];

    fs.readdirSync('/tmp','utf8').forEach(file => {
        console.log(file);
        files.push(file);
    });

    res.download('/tmp/output.txt','output.txt')
}

我认为您的异步函数或文件创建或您的文件存在问题。

【讨论】:

    猜你喜欢
    • 2021-02-09
    • 2020-09-30
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 2020-09-25
    • 2017-09-06
    • 2021-04-25
    相关资源
    最近更新 更多