【问题标题】:How to add GOOGLE_APPLICATION_CREDENTIAL to Vision when hosted outside of GCP在 GCP 外部托管时如何将 GOOGLE_APPLICATION_CREDENTIAL 添加到 Vision
【发布时间】:2023-02-24 04:52:23
【问题描述】:

我正在尝试在 Vercel 上设置无服务器功能,它使用 Google Vision OCR 来注释图像。我能够在本地成功完成,但很难弄清楚如何在 Vision OCR 上添加 GOOGLE_APPLICATION_CREDENTIALS。我已将完整的 service-key.json 文件存储在名为 GOOGLE_APPLICATION_CREDENTIALS 的环境中,但它没有做任何事情。

以下是代码的简要概述:

import vision from '@google-cloud/vision';
const vision = vision.ImageAnnotatorClient();
const analyze = async (req, res) => {
   const [result] = await client.textDetection(req.body.image);
   res.send(result)
} 

我试过使用 google-auth-library 并打印出一个授权令牌,这样我就可以调用 Google REST API,但这也不起作用

【问题讨论】:

    标签: javascript google-cloud-platform google-cloud-vision


    【解决方案1】:

    看完这篇blog,我发现我们可以使用google-auth-library 手动配置凭据,而无需在机器环境中进行设置。在仔细研究之后,我还发现 ImageAnnotatorClient 使用 auth 密钥作为凭据。将它们全部修补在一起后,这是对我有用的最终代码:

    import vision from '@google-cloud/vision';
    import  {GoogleAuth} from 'google-auth-library';
    
    const credentials = JSON.parse(process.env.GOOGLE_APPLICATION_CREDENTIALS);
    const auth = new GoogleAuth({credentials});
    const client = new vision.ImageAnnotatorClient({auth});
    
    const analyze = async (req, res) => {
       const [result] = await client.textDetection(req.body.image);
       res.send(result);
    }
    
    

    正如我的问题中提到的,我将整个 service-key.json 文件从谷歌保存为 vercel env 密钥中的纯字符串对象。然后我只是解析了整个环境变量并将其添加到身份验证生成器中。

    【讨论】:

      猜你喜欢
      • 2020-02-04
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 2021-02-13
      • 2018-03-19
      • 1970-01-01
      • 1970-01-01
      • 2018-05-03
      相关资源
      最近更新 更多