googleapis / nodejs-storage

Node.js client for Google Cloud Storage: unified object storage for developers and enterprises, from live data serving to data analytics/ML to data archiving.
https://cloud.google.com/storage/
Apache License 2.0
888 stars 368 forks source link

cloud storage is authenticated but still cannot use it #2407

Closed idodekerobo closed 4 months ago

idodekerobo commented 4 months ago

when i instantiate gCloudStorage and call the loadCredentials function - i'm seeing data. but when i call it in my app (aka using text speech or something else. it says "Error: Could not load the default credentials"

i did some digging, and even tho when i console.log(gCloudStorage.authClient) it still shows checkIsGCE: undefined. from digigng around in the package code, it seems that you are checking if the GCE .env variable is there for auth and doesn't seem to support/work for passing in the parsed json directly.

this is necessary for hosting in a cloud environment where i'd need to add the json as an .env and can't directly put in file path to json. also seems related to this bug, but not sure if it is exactly the same.

would appreciate any help or feedback! i currently can't use my cloud storage or any of the google cloud compute my app is built on in production.

code snippet

const { Storage } = require("@google-cloud/storage");

const base64EncodedServiceAccount = process.env.CLOUD_CREDENTIALS;
const decodedServiceAccount = Buffer.from(base64EncodedServiceAccount, 'base64').toString('utf-8');
const credentials = JSON.parse(decodedServiceAccount);

const gCloudStorage = new Storage({
   projectId: `palate-d1218`,
   keyFilename: "",
   credentials: credentials
})

async function loadCredentials() {
   console.log(gCloudStorage.authClient)
   try {
      const [buckets] = await gCloudStorage.getBuckets();
      console.log(`buckets: `)
      for (const bucket of buckets) {
         console.log(` - ${bucket.name}`)
      }
      console.log(`listed all buckets`)
   } catch (e) {
      console.log(`error getting buckets`)
      console.log(e)
   }

}
loadCredentials()

Environment details

danielbankhead commented 4 months ago

it seems that you are checking if the GCE .env variable is there for auth and doesn't seem to support/work for passing in the parsed json directly.

Two things here:

import {GoogleAuth} from 'google-auth-library';

const auth = new GoogleAuth({credentials : {}});

Here are instructions for authenticating via Application Default Credentials:

idodekerobo commented 4 months ago

okay it seems i'm authenticated. i'm able to see my storage buckets when loadCredentials runs above, but do you have any idea why am i still seeing errors when trying to call text to speech api? @danielbankhead

i've tried to use the api to output a speech file and to just output voices using googleTextToSpeechClient.listVoices({ languageCode }). i've also double checked that my credentials are compatible with the Cloud Text-to-Speech API in the console of my project.

it also says not to use service account key, but it is not clear on how the app will be authorized in a deployed environment w/o using the keys? but maybe this is user error, but it only works when i add my json directly to my .env file like this GOOGLE_APPLICATION_CREDENTIALS=./firebase-service-account-key.json

i'm getting this error on both -

Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
    at GoogleAuth.getApplicationDefaultAsync (/Users/idodekerobo/Documents/gCloudApp/node_modules/@google-cloud/text-to-speech/node_modules/google-auth-library/build/src/auth/googleauth.js:207:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async GoogleAuth.getClient (/Users/idodekerobo/Documents/gCloudApp/node_modules/@google-cloud/text-to-speech/node_modules/google-auth-library/build/src/auth/googleauth.js:624:17)
    at async GrpcClient._getCredentials (/Users/idodekerobo/Documents/gCloudApp/node_modules/@google-cloud/text-to-speech/node_modules/google-gax/build/src/grpc.js:145:24)
    at async GrpcClient.createStub (/Users/idodekerobo/Documents/gCloudApp/node_modules/@google-cloud/text-to-speech/node_modules/google-gax/build/src/grpc.js:308:23)
(node:95143) MetadataLookupWarning: received unexpected error = Invalid response from metadata service: incorrect Metadata-Flavor header. code = UNKNOWN
(Use `node --trace-warnings ...` to show where the warning was created)
danielbankhead commented 4 months ago

There could be a compatibility issue with the file or it may not be found. Could you try passing the GoogleAuth instance to the Text-to-Speech client?