gpt4 book ai didi

node.js - 使用 Cloud Functions REST API 请求将文件存储在 Firebase Storage 中

转载 作者:太空宇宙 更新时间:2023-11-04 03:25:19 24 4
gpt4 key购买 nike

如何通过 POST(表单数据)HTTP REST 请求在 Firebase Cloud Functions 中存储文件 这样我就可以在 REST 请求之后在云端处理,然后存储在链接到我的项目的存储桶中。

例如,非常容易访问,但存储却不是,我尝试使用 multiparty busboy NodeJS 库,但问题仍然在于我无法将其保存在 Firebase Storage 中.

    var busboy = new Busboy({
headers: req.headers
});
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
file.on('data', function(data) {
// const object = data;
// const fileBucket = object.bucket;
// const bucket = gcs.bucket(filename);
// const tempFilePath = path.join(os.tmpdir(), filename);
console.log(data);
});
file.on('end', function() {
console.log('File [' + fieldname + '] Finished');
});
});
busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
console.log('Field [' + fieldname + ']: value: ' + inspect(val));
});
busboy.on('finish', function() {
console.log('Done parsing form!');
res.writeHead(200, {
Connection: 'close'
});
res.end();
});
req.pipe(busboy);

最佳答案

最后我完成了这个解决方案并且工作正常

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);
});

关于node.js - 使用 Cloud Functions REST API 请求将文件存储在 Firebase Storage 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45534309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com