使用 NodeJS 将文件上传到谷歌云存储 [英] Upload file to google cloud storage with NodeJS

查看:41
本文介绍了使用 NodeJS 将文件上传到谷歌云存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 upload.js 文件包含以下代码:

My upload.js file contains the following code:

module.exports = {

    up: function () {
        const storage = require('@google-cloud/storage');
        const fs = require('fs');
        const gcs = storage({
            projectId: 'MY_PROJECT_ID',
            keyFilename: './service-account.json'
        });
        var bucket = gcs.bucket('MY_BUCKET');

        bucket.upload('picture.jpg', function(err, file) {
            if (err) throw new Error(err);
        });
    },
}

它通过终端工作,但我如何在表单提交按钮单击或仅从不同文件中调用它?

It works through the terminal but how do I call it on form submission button click or just from different file ?

当我尝试它给我:

无法读取未定义的属性原型"

Cannot read property 'prototype' of undefined

我对 NodeJs 很陌生,我真的不知道该怎么做.
不幸的是,Google 文档根本没有帮助我:/

I'm quite new to NodeJs and I don't really know what to do.
Google documentation doesn't help me at all unfortunately :/

推荐答案

我建议将您的 requires 移到第一行.

I would recommend moving your requires to the first line.

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

module.exports = {
    up: function () {
        const gcs = storage({
            projectId: 'MY_PROJECT_ID',
            keyFilename: './service-account.json'
        });
        const bucket = gcs.bucket('MY_BUCKET');
        bucket.upload('picture.jpg', function(err, file) {
            if (err) throw new Error(err);
        });
    },
}

我也会 console.log(storage) 并确认它已定义.

I would also console.log(storage) and confirm it is defined.

这篇关于使用 NodeJS 将文件上传到谷歌云存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆