node.js - Node.js:无法上传到 FirebaseStorage
问题描述
嗨,我正在尝试将图像上传到我的 firebase 存储:
这是我的代码:
exports.uploadImage = (req, res) => {
const BusBoy = require('busboy');
const path = require('path');
const os = require('os');
const fs = require('fs');
const busboy = new BusBoy({ headers: req.headers});
let imageFileName;
let imageToBeUploaded = {};
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
console.log(fieldname);
console.log(filename);
console.log(mimetype);
//image.png
const imageExtension = filename.split('.')[filename.split('.').length - 1];
imageFileName = `${Math.round(Math.random()*10000000).toString()}.${imageExtension}`;
const filepath = path.join(os.tmpdir(), imageFileName);
console.log(filepath);
imageToBeUploaded = {filepath, mimetype};
file.pipe(fs.createWriteStream(filepath));
});
busboy.on('finish', () => {
admin.storage().bucket().upload(imageToBeUploaded.filepath,{
resumable: false,
metadata: {
metadata: {
contentType: imageToBeUploaded.mimetype
}
}
})
.then(() => {
const imageUrl = `https://firebasestorage.googleapis.com/v0/b/${config.storageBucket}/o/${imageFileName}?alt=media`;
console.log(imageUrl);
return db.doc(`/users/${req.user.handle}`).update({ imageUrl: imageUrl});
})
.then(() => {
return res.json({message: 'Image uploaded successfully'});
})
.catch(err => {
console.error(err);
return res.status(500).json({error: err.code})
});
});
busboy.end(req.rawBody);
};
但是我收到以下错误:
> image
> image111.jpeg
> image/jpeg
> C:\Users\adhil\AppData\Local\Temp\8550820.jpeg
> events.js:174
> throw er; // Unhandled 'error' event
> ^
>
> Error: Unexpected end of multipart data
> at C:\Users\adhil\social.ape.proj\socialape-functions\functions\node_modules\dicer\lib\Dicer.js:61:28
> at process._tickCallback (internal/process/next_tick.js:61:11)
> Emitted 'error' event at:
> at Busboy.emit (C:\Users\adhil\social.ape.proj\socialape-functions\functions\node_modules\busboy\lib\main.js:37:33)
> at Dicer.<anonymous> (C:\Users\adhil\social.ape.proj\socialape-functions\functions\node_modules\busboy\lib\types\multipart.js:282:9)
> at Dicer.emit (events.js:198:13)
> at Dicer.emit (C:\Users\adhil\social.ape.proj\socialape-functions\functions\node_modules\dicer\lib\Dicer.js:79:35)
> at C:\Users\adhil\social.ape.proj\socialape-functions\functions\node_modules\dicer\lib\Dicer.js:61:14
> at process._tickCallback (internal/process/next_tick.js:61:11)
我检查了“C:\Users\adhil\AppData\Local\Temp\8550820.jpeg”,发现它是一个 0 MB 的文件。这意味着它尚未成功复制到我的本地目录。
请帮助我了解为什么 fs.createWriteStream 无法正常工作,因为我已多次重新安装该模块。我是 node.js 的新手,所以我希望你能帮助我。
解决方案
被困在同样的问题上。它现在在最新更新的 firebase-tools 7.1.0 中运行。https://github.com/firebase/firebase-tools/issues/1447
推荐阅读
- c# - MySqlDataReader.GetStream() 抛出 IndexOutOfRangeException
- list - 从用户列表创建路径变量
- php - PHP - 在每次方法调用时调用函数
- python - 使用 os.rename 时真的需要指定文件类型吗?
- javascript - 如何使用 Sequelize 以不同的别名和条件将同一个表连接 2 次?
- python - 为什么训练多类语义分割的unet模型中的分类交叉熵损失函数非常高?
- ios - 如何解决二维数组中的“线程 1:致命错误:索引超出范围”
- python - 使用python获取ES索引中的所有文档
- octave - 在 Octave 中计算期望值和方差
- forms - 如何使用 Phoenix 表单在控制器的苦艾酒 @graphql 突变属性中填充自定义类型?