微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法将大文件从 Google Cloud Function 上传到 Google Cloud Storage

如何解决无法将大文件从 Google Cloud Function 上传到 Google Cloud Storage

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

PayloadTooLargeError: request entity too large

看起来云函数会检查上传数据的大小,即使尝试使限制更大。有没有办法超越 10 个月的限制?

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)
    }
})

解决方法

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

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。