问题描述
我需要实现的是允许用户从他的浏览器将文件上传到 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}))