尝试使用node js和busboy将文件上传到firebase存储时出现PayloadTooLargeError



我正在尝试使用firebase云函数、Busboy和node-js将视频文件(14MB(上传到谷歌firebase存储。但我在上传文件时出现了以下错误。此功能适用于小文件,没有任何问题。

PayloadTooLargeError:请求实体太大
在readStream(C:\Users\User\AppData\Roaming\npm\node_modules\firebase tools\node_modeles\raw-body\index.js:155:17(
getRawBody(C:\User\User\AppData \Roaming\npm \node_mmodule \firebase tools\node_modules \raw-body\ index.js:108:12(
读取时(C:\Users\User\AppData\Roaming\npm\node_modules\firebase tools\node_modeles\body-parsers\lib\read.js:77:3(
在rawParser(C:\User\User\AppData\Roaming\npm\node_mmodule \firebase tools\node_modules \body-parser\lib\types\raw.js:81:5(
Layer.handle[asshandle_request]js:95:5(
在trim_preix(C:\Users\User\AppData\Roaming\npm\node_modules\firebase tools\node_modules\express\lib\router\index.js:317:13 2(
下一个(C:\Users\User\AppData\Roaming\npm\node_modules\firebase tools\node_modeles\express\lib\router\index.js:275:10(
在urlendedParser

这些是我的代码文件

index.js

const functions = require("firebase-functions");
const express = require('express');
const FBAuth = require('./util/fbAuth');
const app = express();
const bodyParser = require('body-parser');
const { db } = require("./util/admin");
app.use(express.json({limit: 8 * 1024 * 1024 * 200}));
app.use(express.urlencoded({limit: 8 * 1024 * 1024 * 200}));
const cors = require('cors');
app.use(cors());
app.use(bodyParser.json({limit: 8 * 1024 * 1024 * 200}));
app.use(bodyParser.urlencoded({ limit: 8 * 1024 * 1024 * 200 , extended: true }));
const {
uploadItem
} = require('./handlers/uploads');
app.post('/upload', FBAuth, uploadItem );
exports.api = functions.https.onRequest(app);

上传.js

const { db, bucket } = require('../util/admin');
const BusBoy = require('busboy');
const path = require('path');
const os = require('os');
const fs = require('fs');
const config = require("../util/config");
exports.uploadItem = (req, res) => {
console.log('file upload started')
console.log('data : ' + req.body.name);
const busboy = BusBoy({headers: req.headers});
let dataFileName;
let fileTobeUploaded = {};
let formData = {};
busboy.on(
"field",
(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) => {
formData = { ...formData, [fieldname]: val };
},
);
busboy.on('file',(name, file, info) => {
const { filename, encoding, mimeType } = info;
//my.image.png
const fileExtension = filename.split('.')[filename.split('.').length - 1];
//12345678900.png
dataFileName = `${Math.round(Math.random()*100000000000)}.${fileExtension}`;
const filepath = path.join(os.tmpdir(), dataFileName);
fileTobeUploaded = {filepath, mimeType};
file.pipe(fs.createWriteStream(filepath)); 
})
busboy.on('finish', () => {
req.body = formData;
bucket.upload(fileTobeUploaded.filepath, {
resumable: true,
metadata:{
metadata:{
contentType:fileTobeUploaded.mimeType
}
}
})
.then( () => {
const fileUrl = `https://firebasestorage.googleapis.com/v0/b/${config.storageBucket}/o/${dataFileName}?alt=media`
const newUpload = {
uploadItemUrl: fileUrl,
userHandle: req.user.handle,
userImage: req.user.imageUrl,
createdAt: new Date().toISOString(),
likeCount: 0,
commentCount: 0
};

db.collection('uploads').add(newUpload)
})
.then( () => {

return res.json({message: "File Uploaded Successfully"});
})
.catch(err => {
console.error(err);
return res.status(400).json({ error : err.code});
})
});
busboy.end(req.rawBody);
}

请帮我解决这个问题。提前谢谢。

根据资源限制文档,HTTP函数中可以发送的最大数据限制为10 mb,仅适用于Cloud Function 1st Gen。无法增加此限制。您可以使用Cloud Function(第二代(来获得32mb的限制。您可以在此处查看云功能(第二代(。

但是,您仍然可以让客户端直接上传到存储。在他自己的用户文件夹上进行身份验证,安全规则将文件大小限制为临时文件夹中的任何大小。您可以使用Google云存储或在Web上使用云存储上载文件。

相关内容

  • 没有找到相关文章

最新更新