google 云的服务帐户无法正常工作
Service account with google cloud not working
我正在尝试通过从我的本地计算机创建服务帐户来与 google 云和服务 API 进行通信。我在 Mac OS
我正在关注这篇文章
https://cloud.google.com/docs/authentication/production
我已经在终端的本地会话中设置了 "GOOGLE_APPLICATION_CREDENTIALS" env 变量。
它指向通过google云控制台创建的服务帐户的密钥文件。
但是运行下面的node js程序报错
// 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
由于无法实例化存储对象,读取凭据时似乎出错。
此致,
索拉夫
您必须在 async
函数中使用 await
。假设您创建了一个具有必要权限 (Storage Admin
) 的服务帐户,并导出了环境变量 GOOGLE_APPLICATION_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();
我正在尝试通过从我的本地计算机创建服务帐户来与 google 云和服务 API 进行通信。我在 Mac OS
我正在关注这篇文章
https://cloud.google.com/docs/authentication/production
我已经在终端的本地会话中设置了 "GOOGLE_APPLICATION_CREDENTIALS" env 变量。
它指向通过google云控制台创建的服务帐户的密钥文件。
但是运行下面的node js程序报错
// 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
由于无法实例化存储对象,读取凭据时似乎出错。
此致,
索拉夫
您必须在 async
函数中使用 await
。假设您创建了一个具有必要权限 (Storage Admin
) 的服务帐户,并导出了环境变量 GOOGLE_APPLICATION_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();