【问题标题】:Failed large file upload to Google Cloud Storage from Google Cloud Function将大文件从 Google Cloud Function 上传到 Google Cloud Storage 失败
【发布时间】:2021-04-25 03:11:26
【问题描述】:

我需要实现的是允许用户从他的浏览器将文件上传到谷歌云存储:用户选择一个文件,一个包含该文件的 FormData 被创建并发送到一个云函数。 Cloud Function 然后将数据发送到 Google Cloud Storage。这段代码适用于小文件。但是当我尝试上传更大的文件时(30mo 是我通常想要发送的大小)我有这个错误

PayloadTooLargeError: request entity too large

看起来云功能会检查上传数据的大小,即使尝试使限制更大。有什么办法可以超越 10mo 的限制?

app.use(bodyParser.json({limit: '50mb'}))
app.use(bodyParser.urlencoded({limit: '50mb', extended: true }));

app.post("/upload-my-file", async (req, res) => {
    try {
        await authentChecker(req.headers)
        const busboy = new Busboy({headers: req.headers});

        busboy.on('file', (fieldname, file, filename) => {
            const storage = new Storage();
            const myBucket = storage.bucket('my-bucket-name');
            const newBucketFile = myBucket.file(filename);

            //I don't write a temp file on disk, I directly upload it
            file.pipe(newBucketFile.createWriteStream())
            .on('error', function(err) {
                console.log(err)
            })
            .on('finish', function() {
                console.log("finish")
            });

        })
        // Triggered once all uploaded files are processed by Busboy.
        // We still need to wait for the disk writes (saves) to complete.
        busboy.on('finish', async () => {
            res.send();
        });
        busboy.end(req.rawBody);
    } catch (error) {
        next(error)
    }
})

【问题讨论】:

    标签: node.js express google-cloud-platform google-cloud-storage busboy


    【解决方案1】:

    根据github,您需要为 bodyParser.json 添加“extended: true”。请参阅body-parser module documentation 了解更多信息。

    bodyParser = { json: {limit: '50mb', 扩展: true}, urlencoded:{limit:'50mb',扩展:true} };

    app.use(bodyParser.json({limit: '10mb', extended: true}))

    app.use(bodyParser.urlencoded({limit: '10mb', extended: true}))

    【讨论】:

    • 感谢您的回答,但此解决方案不起作用,我仍然遇到相同的“PayloadTooLargeError”错误。我尝试了人们在这个 github 页面上的建议。看起来他们可能没有使用 Cloud Function,这应该可以解释为什么他们没有这些限制
    猜你喜欢
    • 1970-01-01
    • 2014-09-18
    • 1970-01-01
    • 2018-04-22
    • 2018-12-23
    • 1970-01-01
    • 2020-04-04
    • 2020-06-13
    • 2020-05-05
    相关资源
    最近更新 更多