I am trying to communicate with google cloud and the service APIs by creating a service account from my local machine. i am on Mac OS
I am following this article
https://cloud.google.com/docs/authentication/production
I have set the "GOOGLE_APPLICATION_CREDENTIALS" env variable in my local session of terminal.
It points to the key file of the service account created through google cloud console.
But running the node js program below gives me the error
// 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);
}
Error is
const results = await storage.getBuckets();
^^^^^^^
SyntaxError: Unexpected identifier
Seems to be an error while reading the credentials as the storage object could not be instantiated.
Best Regards,
Saurav
You have to use await
in async
function. Assuming that you created a service account with the necessary permissions (Storage Admin
), and exported the env variable GOOGLE_APPLICATION_CREDENTIALS
then this is the code that worked for me:
'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();