Heroku上的Google Vision API身份验证 [英] Google Vision API authentication on heroku

查看:162
本文介绍了Heroku上的Google Vision API身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发中,我只使用:

@vision = Google::Cloud::Vision.new( project:  "instacult",
                                     keyfile:  "path/to/keyfile.json" )

其中密钥文件是Google创建服务帐户后生成的json( https://cloud.google.com/vision/docs/common/auth ).

Where keyfile is a json produced by google after creating service account (https://cloud.google.com/vision/docs/common/auth).

但是显然我不能只是将密钥文件上传到github.

But obviously I can't just upload the keyfile to github.

我尝试将整个json保存到Heroku的配置变量中并运行:

I tried saving whole json to Heroku's config vars and running:

Rails.env.production? ? ENV["GOOGLE_CREDENTIALS"] : path

但是我在heroku的日志中得到不是有效的文件".似乎合乎逻辑,因为我没有传递文件而是传递了对象.但是如何克服呢?

But I got "is not a valid file" in heroku's logs. Seems logical since I'm not passing a file but an object. But how to get over it?

干杯, 凯

推荐答案

已解决:

结果是您可以在环境变量中提供json对象,但是有一个命名约定.

Turns out you can provide a json object in environment variable, but there is a naming convention.

以下是用于检查的环境变量(按检查顺序) 凭据:

Here are the environment variables (in the order they are checked) for credentials:

  1. VISION_KEYFILE-JSON文件的路径
  2. GOOGLE_CLOUD_KEYFILE-JSON文件的路径
  3. VISION_KEYFILE_JSON-JSON内容
  4. GOOGLE_CLOUD_KEYFILE_JSON-JSON内容
  1. VISION_KEYFILE - Path to JSON file
  2. GOOGLE_CLOUD_KEYFILE - Path to JSON file
  3. VISION_KEYFILE_JSON - JSON contents
  4. GOOGLE_CLOUD_KEYFILE_JSON - JSON contents

: https://googlecloudplatform.github.io/google-cloud-ruby/#/docs/google-cloud-vision/v0.23.0/guides/authentication

所以我最终打电话给了

@vision = Google::Cloud::Vision.new( project:  "instacult")

在我的〜/.bashrc中设置了VISION_KEYFILE_JSON:

Having set VISION_KEYFILE_JSON in my ~/.bashrc:

export VISION_KEYFILE_JSON='the_json_content'

以及在heroku( https://devcenter.heroku.com/articles/config- vars#limits ).

and on heroku (https://devcenter.heroku.com/articles/config-vars#limits).

这篇关于Heroku上的Google Vision API身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆