我正在尝试使用 Google Cloud Vision 执行 Google 反向图像搜索在 Azure 应用服务 Web 应用上。
我已经生成了 googleCred.json,Google 客户端库使用它来构造 API 请求。 Google 希望可以通过名为 GOOGLE_APPLICATION_CREDENTIALS 的环境变量来使用它。
运行 Web 应用的 Azure 应用服务具有模仿 Google 客户端库的环境变量的设置。文档是here ,我已经在这里成功设置了变量:
此外,googleCred.json 文件已上传到应用服务。这是documentation我接着使用FTP和FileZilla上传文件:
此外,文件权限尽可能开放:
但是,当我访问云端的 Web 应用程序时,收到以下错误消息:
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?
最佳答案
当应用程序由于文件丢失、凭据路径无效、环境变量分配不正确等多种原因而无法正确身份验证时,通常会抛出此错误消息。
基于此,我建议您验证凭证文件和文件路径是否已正确分配,并遵循 Obtaining and providing service account credentials manually指导以便直接在代码中明确指定您的服务帐户文件;通过这种方式,您将能够永久设置它并验证您是否正确传递了服务凭据。
在代码示例中传递服务帐户 key 的路径:
// 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);
});
关于azure - Google Cloud Vision 反向图像搜索在 Azure 应用服务上失败,因为找不到 GOOGLE_APPLICATION_CREDENTIALS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52186137/