简体   繁体   中英

Store file in Firebase Storage using Cloud Functions REST API request

How do I store a file through a POST (form-data) HTTP REST request in Firebase Cloud Functions so I can handle in the cloud after the REST request and then store in a bucket linked to my project.

For example is very easy to access but storage is not, I've tried to use multiparty and busboy NodeJS libs but the problem still in that Im not able to save it in 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);

At the end I finish doing this solution and works fine

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

Easiest way to access fireabse/google cloud storage from a firebase function is to use the node js google cloud package for the service, in this case @google-cloud/storage .

Once its part of your functions' dependencies you simply start it up within your function js file:

const gcs = require('@google-cloud/storage')();

Because the function runs in the google cloud environment there is no need to provide authentication info (unless storage and functions aren't part of the same Google Cloud/Firebase projects).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM