尝试使用节点 js 和 busboy 将文件上传到 firebase 存储时出现 PayloadTooLargeError
Got PayloadTooLargeError when try to upload file to firebase storage using node js and busboy
我正在尝试使用 firebase 云功能、Busboy 和节点 js 将视频文件 (14 MB) 上传到 google firebase 存储。但是我在文件上传过程中遇到了以下错误。此功能适用于小文件,没有任何问题。
PayloadTooLargeError: request entity too large
at readStream (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\raw-body\index.js:155:17)
at getRawBody (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\raw-body\index.js:108:12)
at read (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\read.js:77:3)
at rawParser (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\types\raw.js:81:5)
at Layer.handle [as handle_request] (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\index.js:317:13)
at C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\index.js:275:10)
at urlencodedParser (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\types\urlencoded.js:100:7)
这些是我的代码文件
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);
upload.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);
}
请帮我解决这个问题。提前谢谢你。
根据Resource Limit Documentation, the maximum data limit can be sent in HTTP Function is 10 mb only for Cloud Function 1st Gen. There is no way to get this limit increased. You could use Cloud Function (2nd gen) to get a 32mb limit instead. You may check the Cloud Function (2nd gen) here。
不过,您仍然可以让客户端直接上传到存储。在他自己的用户文件夹和安全规则上进行身份验证,将文件大小限制为您希望放入临时文件夹的任何大小。您可以使用 Google Cloud Storage or Upload files with Cloud Storage on Web.
我正在尝试使用 firebase 云功能、Busboy 和节点 js 将视频文件 (14 MB) 上传到 google firebase 存储。但是我在文件上传过程中遇到了以下错误。此功能适用于小文件,没有任何问题。
PayloadTooLargeError: request entity too large
at readStream (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\raw-body\index.js:155:17)
at getRawBody (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\raw-body\index.js:108:12)
at read (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\read.js:77:3)
at rawParser (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\types\raw.js:81:5)
at Layer.handle [as handle_request] (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\index.js:317:13)
at C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\router\index.js:275:10)
at urlencodedParser (C:\Users\User\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\body-parser\lib\types\urlencoded.js:100:7)
这些是我的代码文件
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);
upload.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);
}
请帮我解决这个问题。提前谢谢你。
根据Resource Limit Documentation, the maximum data limit can be sent in HTTP Function is 10 mb only for Cloud Function 1st Gen. There is no way to get this limit increased. You could use Cloud Function (2nd gen) to get a 32mb limit instead. You may check the Cloud Function (2nd gen) here。
不过,您仍然可以让客户端直接上传到存储。在他自己的用户文件夹和安全规则上进行身份验证,将文件大小限制为您希望放入临时文件夹的任何大小。您可以使用 Google Cloud Storage or Upload files with Cloud Storage on Web.