代码之家  ›  专栏  ›  技术社区  ›  Prince Hamza

如何为谷歌云存储设置nodejs环境

  •  0
  • Prince Hamza  · 技术社区  · 7 年前

    我是谷歌云平台的新手,我正在学习本教程

    https://cloud.google.com/docs/authentication/getting-started#auth-cloud-implicit-nodejs

    在设置了环境变量后,我使用了谷歌云,这一行导致了问题

    const {Storage} = require('@google-cloud/storage');
    

    当我对此行进行注释时,它会起作用,否则会引发错误。

    TypeError: Expected `input` to be a `Function` or `Object`, got `undefined`
    push../node_modules/gtoken/node_modules/pify/index.js.module.exports
    D:/SE/Web/React/storybook/node_modules/gtoken/node_modules/pify/index.js:56
    53 | var objType = typeof input;
    54 | 
    55 | if (!(input !== null && (objType === 'object' || objType === 
    'function'))) {
    > 56 |   throw new TypeError("Expected `input` to be a `Function` or 
    `Object`, got `".concat(input === null ? 'null' : objType, "`"));
     | ^  57 | }
     58 | 
     59 | var filter = function filter(key) {
     View compiled
     ./node_modules/gtoken/build/src/index.js
     D:/SE/Web/React/storybook/node_modules/gtoken/build/src/index.js:65
     62 | 
     63 | var pify = require("pify");
     64 | 
     > 65 | var readFile = pify(fs.readFile);
     66 | var GOOGLE_TOKEN_URL = 'https://www.googleapis.com/oauth2/v4/token';
     67 | var GOOGLE_REVOKE_TOKEN_URL = 
    'https://accounts.google.com/o/oauth2/revoke?token=';
     68 | 
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Andrei Cusnir    7 年前

    我遵循了和你上面提到的相同的教程。这对我很有用。请确保过程中没有跳过任何步骤。

    1. 重要的是 Create a service account 下载 JSON文件到您的计算机。然后将JSON文件上传到 云壳。
    2. 设置 GOOGLE_APPLICATION_CREDENTIALS CloudShell中的变量,指向json文件的路径。按照上的说明操作 Setting the environment variable .
    3. 创建一个 Virtual Environment . 执行 virtualenv [ENVIRONMENT_NAME] .
    4. 通过执行激活环境 source [ENVIRONMENT_NAME]/bin/activate .
    5. 安装 @google-cloud/storage 图书馆。执行 npm install --save @google-cloud/storage .
    6. 创建一个 Nodejs 文件。添加代码窗体 Getting Started with Authentication > Verifying authentication 文档。
    7. 通过执行来运行文件 node [FILE_NAME].js .

    如果出现错误:

    错误:[GSServiceAccount]没有storage.buckets.list访问项目[project\u number]。

    1. 然后复制整个 [GSERVICEACCOUNT] .
    2. 去找你 Google Cloud Console .
    3. IAM & admin 导航菜单中的页面。
    4. 点击 Add+ .
    5. New members 添加 [G服务帐户] .
    6. 形成下拉菜单 Select a role 选择 Storage Admin 角色。
    7. 点击 Save .

    现在,如果您再次执行 节点[文件名].js . 您应该看到云外壳上列出的所有桶。