node.js - 通过 node.js 在当前 shell session 中设置 GOOGLE_APPLICATION_CREDENTIALS

标签 node.js google-cloud-platform google-cloud-storage

概述:

我正在通过 Node.js v8.12.0 将产品部署到 GCP 以及随后的 GCS 中构建自动化。此部署过程的开始要求我执行以下操作:

  1. 遍历项目文件夹中的文件夹和文件,找到GCS服务帐户JSON key 文件。

  2. 确定所述 key 文件的位置。

  3. 运行 export GOOGLE_APPLICATION_CREDENTIALS="/path/to/file/[FILE_NAME].json"以设置位置,以便我可以执行代码以通过 Node 调用 GCS 函数。

我尝试通过node.js 中的child_process.exec 模块执行命令。执行成功,但在备用 shell 中。

根据 Google 文档,设置 GOOGLE_APPLICATION_CREDENTIALS 变量“...仅适用于您当前的 shell session ,因此如果您打开新 session ,请再次设置该变量。”

虽然不需要花费大量时间来设置,但我希望在部署过程中自动执行此操作。那么有没有办法让我在当前的 shell 中执行这个?也对其他想法持开放态度。

代码如下:

const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
var readFileTree = require('read-file-tree');
const prompts = require('prompts');
const find = require('find-file-up');
// var tty = require('tty');
var program = require('commander');
const { exec } = require('child_process');
const chalk = require('chalk');
var gsBuckets = {};
var i = 0;
    

function findOauthJSON () {
console.log("Finding your service account keys...")
 find('fauxfile_1123456ae.json', 'a/b/c', function(err, file) {
     if (file !== undefined) {
           console.log("Service keys found: ",file);
           setOathJSON(file);
     } else if (err !== undefined) {
         
         console.log(err);
     } else {
         
         console.log("Couldn't find your service account keys.  Add to your folders and try again.")
     }

});

}

function setOathJSON (file) {
    console.log("Setting service key pay to access GCS...");


var command = 'export GOOGLE_APPLICATION_CREDENTIALS=\"'+file+'\"';

exec(command, (err, stdout, stderr) => {
  if (err === null) {
    console.log("Service key set!");
  }

  // the *entire* stdout and stderr (buffered)
  console.log(`stdout: ${stdout}`);
  console.log(`stderr: ${stderr}`);
  console.log(`err: ${err}`);
  console.log("cmd: "+command);
});
}

function gstoreInfo () {
  
console.log("Getting cloud storage info...");
storage
  .getBuckets()
  .then(results => {
    const buckets = results[0];
    
    buckets.forEach(bucket => {
      gsBuckets["gstoreb"+i] = bucket.name;
      console.log("Found bucket: "+bucket.name);
      i++;
    });
  })
    .catch(err => {
    console.error('ERROR:', err);
  });
  
}

最佳答案

想通了。 Node 惯用库允许您通过实例化的存储对象设置 keyFileName 路径,如下所示:

const storage = new Storage({
  projectId: 'mystorage-bucket-123456',
  keyFilename: ''/path/to/keyfile.json''
});

此文档有点隐藏,但您可以在这里找到它:https://github.com/googleapis/google-cloud-node/blob/master/docs/authentication.md

每当您调用存储时,都会传递 key 文件名以进行身份​​验证。我使用了与上面相同的代码并且它有效。

关于node.js - 通过 node.js 在当前 shell session 中设置 GOOGLE_APPLICATION_CREDENTIALS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52635632/

相关文章:

google-bigquery - 如何在Google云平台中自动加载CSV文件?

ssl - 使用 cloud_proxy_sql 设置自定义 CA 证书

kubernetes - 没有 Google 云存储的 Kubeflow

node.js - 在 node.js 中使用 socket.io 的 redis pub/sub

node.js - 如何用mongoose和nodejs进行同步调用

google-cloud-platform - 通过 CLI 编写没有 header 的 Google 存储对象

php - 谷歌云存储文档缩略图?

java - 谷歌云存储md5检查文件

node.js - 尝试使用访问 token 访问 Spotify Web api 时收到 401

node.js - 我应该在哪里安装/保存node_modules?