0

我意识到这可能是一个愚蠢的问题,所以感谢您花时间阅读(并希望能回答)它。我对 Javascript 和节点都是新手,所以请假设我知道一些行话。

我正在尝试使用谷歌客户端库中的 table.delete() 函数(链接如下)删除 node.js(v12.15.0)中的 Big Query 表。

https://googleapis.dev/nodejs/bigquery/latest/Table.html#delete

const {BigQuery} = require('@google-cloud/bigquery');
const bigquery = new BigQuery();

async function deleteTable(
  datasetId = 'my_dataset',
  tableId = 'my_table',
) {
  console.log(`Deleting table ${tableId}`);
  try {
    const [table] = await bigquery
    .dataset(datasetId)
    .table(tableId)
    .delete();
    console.log(`Table ${tableId} deleted.`);
  } catch (err){
    console.log(err);
  }
}

deleteTable();

要运行此程序,谷歌需要从您的 Big Query 帐户进行身份验证,我已将其作为 json 文件下载并存储在“凭据”文件夹中。该路径作为环境变量保存在我的 ~/.bash_profile 中:

export GOOGLE_APPLICATION_CREDENTIALS="/Users/tom/Desktop/work/test/credentials/google_application_credentials.json"

当我尝试运行上述脚本时,出现 ENOENT 错误:

Error: The file at {[contents of google_application_credentials.json]} does not exist, or it is not a file. 
ENOENT: no such file or directory, lstat '/Users/tom/Desktop/work/test/{[contents of google_application_credentials.json]}
   at Object.realpathSync (fs.js:1529:7)
    at GoogleAuth._getApplicationCredentialsFromFilePath (/Users/tom/Desktop/work/test/node_modules/google-auth-library/build/src/auth/googleauth.js:250:27)
    at GoogleAuth._tryGetApplicationCredentialsFromEnvironmentVariable (/Users/tom/Desktop/work/test/node_modules/google-auth-library/build/src/auth/googleauth.js:192:25)
    at GoogleAuth.getApplicationDefaultAsync (/Users/tom/Desktop/work/test/node_modules/google-auth-library/build/src/auth/googleauth.js:130:33)
    at GoogleAuth.getClient (/Users/tom/Desktop/work/test/node_modules/google-auth-library/build/src/auth/googleauth.js:502:28)
    at GoogleAuth.authorizeRequest (/Users/tom/Desktop/work/test/node_modules/google-auth-library/build/src/auth/googleauth.js:543:35)
    at BigQuery.makeAuthenticatedRequest (/Users/tom/Desktop/work/test/node_modules/@google-cloud/common/build/src/util.js:374:28)
    at BigQuery.request_ (/Users/tom/Desktop/work/test/node_modules/@google-cloud/common/build/src/service.js:129:18)
    at BigQuery.request (/Users/tom/Desktop/work/test/node_modules/@google-cloud/common/build/src/service.js:140:36)
    at Dataset.request_ (/Users/tom/Desktop/work/test/node_modules/@google-cloud/common/build/src/service-object.js:231:21) {
  errno: -2,
  syscall: 'lstat',
  code: 'ENOENT',
  path: '/Users/tom/Desktop/work/test/{[contents of google_application_credentials.json]}

显然,这个脚本在 /Users/tom/Desktop/work/test 中搜索 .json 文件的内容,而不是文件本身,但我不知道为什么。bash配置文件中的路径肯定是正确的,环境变量也正确更新了。如果有人能帮我一把,我将不胜感激!如果您需要更多信息,请告诉我。

4

1 回答 1

1

已解决:我重新启动了我的笔记本电脑,它肯定已经清除了一些不应该存在的环境变量,然后下载了一个新的 .json 文件,其中包含来自 Big Query 的帐户身份验证详细信息。值得注意的是,这些已经改变(我认为它们会随着每次下载而改变),所以我可能事先使用了一个过时的文件。

这使得脚本可以正常工作。

如果有人对实际发生的事情有更好的答案,请回答!

于 2020-02-12T17:01:54.380 回答