Google Cloud Vision 反向图像搜索在 Azure App Service 上失败,因为找不到 GOOGLE_APPLICATION_CREDENTIALS 文件

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

我正尝试在 Azure 应用服务 Web 应用上使用 Google Cloud Vision 执行 Google 反向图像搜索。

我生成了一个 googleCred.json,Google 客户端库使用它来构造 API 请求。 Google 期望它可以从名为 GOOGLE_APPLICATION_CREDENTIALS.

的环境变量中获得

运行 Web 应用程序的 Azure 应用程序服务具有模仿 Google 客户端库的环境变量的设置。文档是here,我这里设置变量成功:

此外,googleCred.json文件已上传到应用服务。这是我使用 FTP 和 FileZilla 上传文件的 documentation

另外,文件权限尽可能开放:

但是,当我访问云中的网络应用程序时,出现以下错误消息:

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

我做错了什么?如何在 Azure Web 应用程序上成功使用 Google Cloud Vision API?

我写在这里是因为我无法发表评论,但快速浏览一下,路径中的 "D:" 是否必要?我假设您已将文件上传到应用程序服务,因此请尝试使用此值作为路径“\site\wwwroot\Statics\googleCred.json”

此错误消息通常在应用程序未被正确身份验证时抛出,原因有多种,例如丢失文件、无效凭证路径、不正确的环境变量分配等。 .

基于此,我建议您验证是否正确分配了凭据文件和文件路径,并遵循 Obtaining and providing service account credentials manually 指南以明确指定您的服务帐户file 直接进入你的代码;这样,您将能够永久设置它并验证您是否正确传递了服务凭据。

在代码示例中将路径传递给服务帐户密钥:

// 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);
  });