Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google cloud platform 谷歌云服务帐户不工作_Google Cloud Platform_Google Cloud Storage_Google Auth Library - Fatal编程技术网

Google cloud platform 谷歌云服务帐户不工作

Google cloud platform 谷歌云服务帐户不工作,google-cloud-platform,google-cloud-storage,google-auth-library,Google Cloud Platform,Google Cloud Storage,Google Auth Library,我试图通过从本地机器创建一个服务帐户来与谷歌云和服务API进行通信。我使用Mac OS 我正在关注这篇文章 我已经在终端的本地会话中设置了“GOOGLE\u APPLICATION\u CREDENTIALS”env变量 它指向通过谷歌云控制台创建的服务帐户的密钥文件 但是运行下面的NodeJS程序会产生错误 // Imports the Google Cloud client library. const {Storage} = require('@google-cloud/stor

我试图通过从本地机器创建一个服务帐户来与谷歌云和服务API进行通信。我使用Mac OS

我正在关注这篇文章

我已经在终端的本地会话中设置了“GOOGLE\u APPLICATION\u CREDENTIALS”env变量

它指向通过谷歌云控制台创建的服务帐户的密钥文件

但是运行下面的NodeJS程序会产生错误

    // Imports the Google Cloud client library.
const {Storage} = require('@google-cloud/storage');

// Instantiates a client. If you don't specify credentials when constructing
// the client, the client library will look for credentials in the
// environment.
const storage = new Storage();

try {
  // Makes an authenticated API request.
  const results = await storage.getBuckets();

  const [buckets] = results;

  console.log('Buckets:');
  buckets.forEach(bucket => {
    console.log(bucket.name);
  });
} catch (err) {
  console.error('ERROR:', err);
}
错误是

    const results = await storage.getBuckets();
                        ^^^^^^^

SyntaxError: Unexpected identifier
读取凭据时似乎出错,因为无法实例化存储对象

致以最良好的祝愿


Saurav

您必须在
异步
功能中使用
等待
。假设您创建了一个具有必要权限的服务帐户(
Storage Admin
),并导出了env变量
GOOGLE\u APPLICATION\u CREDENTIALS
),那么这就是适用于我的代码:


   'use strict';

const authCloudImplicit = async () => {
  // [START auth_cloud_implicit]
  // Imports the Google Cloud client library.
  const {Storage} = require('@google-cloud/storage');

  // Instantiates a client. If you don't specify credentials when constructing
  // the client, the client library will look for credentials in the
  // environment.
  const storage = new Storage();

  try {
    // Makes an authenticated API request.
    const results = await storage.getBuckets();

    const [buckets] = results;

    console.log('Buckets:');
    buckets.forEach(bucket => {
      console.log(bucket.name);
    });
  } catch (err) {
    console.error('ERROR:', err);
  }
  // [END auth_cloud_implicit]
};

const a = authCloudImplicit();


非常感谢…错过了简单的事情…它现在可以工作了