问题描述
我正在将multer与nodejs一起使用以处理多部分表单数据.我不想保存从client获得的req.file.我想直接将内存中的文件缓冲区上传到Google云存储中...
I'm using multer with nodejs to handle multipart form data . I don't want to save the req.file which i get from client . I want to directly upload the file buffer in memory to google cloud storage ...
但是存储桶的(firebase存储)上载方法仅采用文件路径作为参数,是否有任何方法可以直接实现此目的而无需保存文件并将文件缓冲区直接上传到firebase存储器中?
But the storage bucket's (firebase storage) upload method takes only a file path as argument.Is there any way i can achieve this directly without saving the file and upload the file buffer in memory to firebase storage directly ?
如果是这样,该怎么做?
If so , how to do that ?
推荐答案
该解决方案就在nodejs的云存储入门指南中.
The solution is right there in the cloud storage getting started guide for nodejs.
function sendUploadToGCS (req, res, next) {
if (!req.file) {
return next();
}
const gcsname = Date.now() + req.file.originalname;
const file = bucket.file(gcsname);
const stream = file.createWriteStream({
metadata: {
contentType: req.file.mimetype
},
resumable: false
});
stream.on('error', (err) => {
req.file.cloudStorageError = err;
next(err);
});
stream.on('finish', () => {
req.file.cloudStorageObject = gcsname;
file.makePublic().then(() => {
req.file.cloudStoragePublicUrl = getPublicUrl(gcsname);
next();
});
});
stream.end(req.file.buffer);
}
参考: https://cloud.google.com/nodejs/getting-started/using-cloud-storage
这篇关于如何将内存中的multer文件缓冲区上载到Google Cloud Storage Bucket?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!