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

标签 node.js rest firebase firebase-storage

如何通过 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/

相关文章:

java - Web 服务与 Servlet

firebase - RNFirebase 邮箱验证

node.js - Node 事件处理

node.js - 如何更新mongodb native nodejs驱动程序中的多个文档?

rest - Visual Studio 无法在我的 rest api 客户端中添加 swagger 元数据文件

java - 向服务器发送 DELETE 时出现错误 405 方法不允许错误

firebase - 阻止禁用用户 firebase 规则

sorting - firebase 和 Ionic 2 上的降序 orderByChild()

mysql - 无法在 Sequelize.js 中定义 hasMany() 关联?

javascript - armv6l 上的 meteor (树莓派)