Node.js 使用云函数REST API请求将文件存储在Firebase存储中
如何通过POST(表单数据)HTTP REST请求在中存储文件,以便在REST请求后在云中处理,然后存储在链接到我的项目的bucket中 例如,访问非常容易,但存储却不容易,我尝试使用和NodeJS libs,但问题仍然在于我无法将其保存在中Node.js 使用云函数REST API请求将文件存储在Firebase存储中,node.js,rest,firebase,firebase-storage,Node.js,Rest,Firebase,Firebase Storage,如何通过POST(表单数据)HTTP REST请求在中存储文件,以便在REST请求后在云中处理,然后存储在链接到我的项目的bucket中 例如,访问非常容易,但存储却不容易,我尝试使用和NodeJS libs,但问题仍然在于我无法将其保存在中 从firebase函数访问fireabse/google cloud存储的最简单方法是使用node js google cloud包进行服务,在本例中 一旦它成为函数依赖项的一部分,您只需在函数js文件中启动它: const gcs=require('@g
从firebase函数访问fireabse/google cloud存储的最简单方法是使用node js google cloud包进行服务,在本例中 一旦它成为函数依赖项的一部分,您只需在函数js文件中启动它:
const gcs=require('@googlecloud/storage')()代码>
由于该功能在google cloud环境中运行,因此无需提供身份验证信息(除非存储和功能不是同一google cloud/Firebase项目的一部分)。从Firebase功能访问fireabse/google cloud存储的最简单方法是将node js google cloud包用于该服务,在这种情况下
一旦它成为函数依赖项的一部分,您只需在函数js文件中启动它:
const gcs=require('@googlecloud/storage')()代码>
由于该功能在google cloud环境中运行,因此无需提供身份验证信息(除非存储和功能不是同一google cloud/Firebase项目的一部分)。最后,我完成了此解决方案,效果良好
router.post('/upload', function (req, res) {
var busboy = new Busboy({headers: req.headers});
var files = 0, finished = false;
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
console.log('File [' + fieldname + ']: filename: ' + filename);
console.log("Uploading: " + filename);
++files;
var path = temp.writeFileSync(file);
var fstream = fs.createWriteStream(path);
fstream.on('finish', function () {
if (--files === 0) {
var bucket = firebase.bucket();
// Upload a local file to a new file to be created in your bucket.
bucket.upload(path+"",function (err, file) {
if (!err) {
console.log("Uploaded: " + path);
res.json({
uploaded: true,
created_at: new Date().getTime(),
filename: filename,
path: path,
mimeType: mimetype
});
}else{
console.error("err: " + err);
var error = new ErrorResponse(400);
error.errors+=err;
res.json(error);
}
});
}
});
file.pipe(fstream);
});
最后,我完成了这个解决方案,效果很好
router.post('/upload', function (req, res) {
var busboy = new Busboy({headers: req.headers});
var files = 0, finished = false;
busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
console.log('File [' + fieldname + ']: filename: ' + filename);
console.log("Uploading: " + filename);
++files;
var path = temp.writeFileSync(file);
var fstream = fs.createWriteStream(path);
fstream.on('finish', function () {
if (--files === 0) {
var bucket = firebase.bucket();
// Upload a local file to a new file to be created in your bucket.
bucket.upload(path+"",function (err, file) {
if (!err) {
console.log("Uploaded: " + path);
res.json({
uploaded: true,
created_at: new Date().getTime(),
filename: filename,
path: path,
mimeType: mimetype
});
}else{
console.error("err: " + err);
var error = new ErrorResponse(400);
error.errors+=err;
res.json(error);
}
});
}
});
file.pipe(fstream);
});
我让firebase存储运行问题是我使用HTTP Post多部分执行请求,但我无法将其临时存储在我使用NodeJS处理Post请求然后将其保存在google存储的函数中。我让firebase存储运行问题是我使用HTTP Post多部分执行请求,但是我不能将它暂时存储在我使用NodeJS处理post请求然后将其保存在google存储中的函数中。