Search code examples
azuregoogle-apiazure-web-app-servicegoogle-cloud-visionazure-app-service-plans

Google Cloud Vision reverse image search fails on Azure App Service because GOOGLE_APPLICATION_CREDENTIALS file cannot be found


I am attempting to perform a Google reverse image search using Google Cloud Vision on an Azure app service web app.

I have generated a googleCred.json, which the Google client libraries use in order to construct API requests. Google expects it to be available from an environment variable named GOOGLE_APPLICATION_CREDENTIALS.

The Azure app service that runs the web app has settings that mimic environment variables for the Google client libraries. The documentation is here, and I have successfully set the variable here:

enter image description here

Furthermore, the googleCred.json file has been uploaded to the app service. Here is the documentation I followed to use FTP and FileZilla to upload the file:

enter image description here

Also, the file permissions are as open as they can be:

enter image description here

However, when I access the web app in the cloud, I get the following error message:

Error reading credential file from location D:\site\wwwroot\Statics\googleCred.json: Could not find a part of the path 'D:\site\wwwroot\Statics\googleCred.json'. Please check the value of the Environment Variable GOOGLE_APPLICATION_CREDENTIALS

What am I doing wrong? How can I successfully use the Google Cloud Vision API on an Azure web app?


Solution

  • This error message is usually thrown when the application is not being authenticated correctly due to several reasons such as missing files, invalid credential paths, incorrect environment variables assignations, among other causes.

    Based on this, I recommend you to validate that the credential file and file path are being correctly assigned, as well as follow the Obtaining and providing service account credentials manually guide in order to explicitly specify your service account file directly into your code; In this way, you will be able to set it permanently and verify if you are passing the service credentials correctly.

    Passing the path to the service account key in code example:

    // Imports the Google Cloud client library.
    const Storage = require('@google-cloud/storage');
    
    // Instantiates a client. Explicitly use service account credentials by
    // specifying the private key file. All clients in google-cloud-node have this
    // helper, see https://github.com/GoogleCloudPlatform/google-cloud-node/blob/master/docs/authentication.md
    const storage = new Storage({
      keyFilename: '/path/to/keyfile.json'
    });
    
    // Makes an authenticated API request.
    storage
      .getBuckets()
      .then((results) => {
        const buckets = results[0];
    
        console.log('Buckets:');
        buckets.forEach((bucket) => {
          console.log(bucket.name);
        });
      })
      .catch((err) => {
        console.error('ERROR:', err);
      });