Cloud Functions 设置确实发生了重大变化,触发了此问题。它与中间件的工作方式有关,该方式应用于所有用于提供 HTTPS 功能的 Express 应用程序(包括默认应用程序)。基本上,Cloud Functions 将解析请求的正文并决定如何处理它,将正文的原始内容留在req.rawBody. 您可以使用它来直接解析您的多部分内容,但您不能使用中间件(如 multer)来做到这一点。
相反,您可以使用名为busboy的module直接处理原始正文内容。它可以接受rawBody缓冲区并用它找到的文件给你回电。下面是一些示例代码,它将迭代所有上传的内容,将它们保存为文件,然后删除它们。您显然会想做一些更有用的事情。
const path = require('path');
const os = require('os');
const fs = require('fs');
const Busboy = require('busboy');
exports.upload = functions.https.onRequest((req, res) => {
if (req.method === 'POST') {
const busboy = new Busboy({ headers: req.headers });
// This object will accumulate all the uploaded files, keyed by their name
const uploads = {}
// This callback will be invoked for each file uploaded
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
console.log(`File [${fieldname}] filename: ${filename}, encoding: ${encoding}, mimetype: ${mimetype}`);
// Note that os.tmpdir() is an in-memory file system, so should only
// be used for files small enough to fit in memory.
const filepath = path.join(os.tmpdir(), fieldname);
uploads[fieldname] = { file: filepath }
console.log(`Saving '${fieldname}' to ${filepath}`);
file.pipe(fs.createWriteStream(filepath));
});
// This callback will be invoked after all uploaded files are saved.
busboy.on('finish', () => {
for (const name in uploads) {
const upload = uploads[name];
const file = upload.file;
res.write(`${file}\n`);
fs.unlinkSync(file);
}
res.end();
});
// The raw bytes of the upload will be in req.rawBody. Send it to busboy, and get
// a callback when it's finished.
busboy.end(req.rawBody);
} else {
// Client error - only support POST
res.status(405).end();
}
})
请记住,保存到临时空间的文件会占用内存,因此它们的总大小应限制为 10MB。对于较大的文件,您应该将它们上传到 Cloud Storage 并使用存储触发器处理它们。
另请记住,由 Cloud Functions 添加的默认中间件选择当前未通过firebase serve. 因此,在这种情况下,此示例将不起作用(rawBody 将不可用)。
该团队正在更新文档,以更清楚地了解与标准 Express 应用程序不同的 HTTPS 请求期间发生的一切。