如何将文件上传到Node.js中的GCloud?

时间:2016-12-19 21:45:35

标签: javascript node.js firebase google-cloud-storage multer

我看到了什么:

想法:

我目前在本地存储图像并将图像路径存储在Firebase实时数据库中。我想将图像存储在GCloud中(以降低成本)。

问题:

我应该如何修改我的当前代码以上传到GCLOUD?

当前代码:

var express = require("express");
var router = express.Router();
var flash = require("connect-flash");

var firebase = require("firebase");

var multer = require("multer");
var upload = multer({dest:"./public/images/uploads/", limits: {fileSize: 250000}}).single("image");


router.get("/", function(req, res, next){

    if (firebase.auth().currentUser == null) {
        global.page_name = "login";
        res.render("users/login");
    }
    else {
        global.page_name = "upload";
        res.render("upload");
    }
});

router.post("/", function(req, res, next){
    upload(req,res, function (err) {
        if (err) {
            req.flash("error_msg", "File too large. Max Size: 250KB");
            res.redirect("/upload"); 
            return
        }


        var title = req.body.title;
        var section = req.body.section;

        // Validation
        req.checkBody('title', 'Title is required').notEmpty();
        req.checkBody('title', 'Title is too long, max: 50 charachters !').len(0,50);
        req.checkBody('section', 'Section is required').notEmpty();

        var errors = req.validationErrors();

        if(errors){
            res.render('upload', {
                errors: errors
            });
        }
        else {

            if (req.file){

                console.log("Uploading file...");
                var image = req.file.filename;
                var section = req.body.section.toLowerCase();

                var IDref = firebase.database().ref("posts/"+section+"/lastID");

                IDref.once("value", function(IDsnapshot){
                    var number = IDsnapshot.val();
                    var postRef = firebase.database().ref("posts/"+section).push();
                    var UID = postRef.key;
                    var post = {
                        title: req.body.title,
                        section: req.body.section,
                        image: image,
                        author: firebase.auth().currentUser.uid,
                        section:  section,
                        upvotes: 0,
                        downvotes: 0,
                        id: number + 1,
                        key: UID
                    }

                    IDref.set(post.id);

                    postRef.set(post);

                    var authoredPost = {
                        title: req.body.title,
                        section: section,
                    }

                    var userRef = firebase.database().ref("users/"+firebase.auth().currentUser.uid.toString()+"/posts/"+UID.toString());
                    userRef.set(authoredPost);

                });

                req.flash("success_msg", "Post Created");
                res.redirect("/upload"); 
            }
            else {
                console.log("No file uploaded");
                req.flash("error_msg", "Image is required");
                res.redirect("/upload");
            }
        }
    });
});

module.exports = router;

1 个答案:

答案 0 :(得分:6)

我认为最好在网络或应用中使用firebase.storage直接上传到GCloud,但您可以使用此代码来解决您的问题

contenteditable="false"