Google cloud platform 谷歌云服务帐户不工作
我试图通过从本地机器创建一个服务帐户来与谷歌云和服务API进行通信。我使用Mac OS 我正在关注这篇文章 我已经在终端的本地会话中设置了“GOOGLE\u APPLICATION\u CREDENTIALS”env变量 它指向通过谷歌云控制台创建的服务帐户的密钥文件 但是运行下面的NodeJS程序会产生错误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
// 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();
非常感谢…错过了简单的事情…它现在可以工作了