嗨,我正在尝试将图像上传到我的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的新手,所以希望您能为我提供帮助。
答案 0 :(得分:1)
被困在同一问题上。 firebase-tools 7.1.0的最新更新现在正在运行。 https://github.com/firebase/firebase-tools/issues/1447