【发布时间】:2020-09-16 17:58:26
【问题描述】:
我正在使用 Spring Boot 开发一个 POC 网站,以了解 GCP 产品,特别是 Google Cloud Storage。上下文是我正在尝试在用户注册时保存个人资料图片。
从谷歌文档中,我可以使用
StorageOptions.getDefaultInstance().getService();
如果我的环境变量中有 GOOGLE_APPLICATION_CREDENTIALS,则验证我的凭据。事实是,我确实将 GOOGLE_APPLICATION_CREDENTIALS 作为环境变量(我使用的是 Linux Mint)指向我的 .json 文件(这是有效的,因为它使用路径方法有效),但它总是从 GCP lib 返回 401 UNAUTHORIZED。
花费 HOURS 搜索后,只是为了确保我在终端上运行此命令:
gcloud auth application-default login
这个命令的响应是:
The environment variable [GOOGLE_APPLICATION_CREDENTIALS] is set to:
[/home/<myuser>/<some_folder>/<myapplication>-<id>.json]
Credentials will still be generated to the default location:
[/home/<myuser>/.config/gcloud/application_default_credentials.json]
To use these credentials, unset this environment variable before
running your application.
Do you want to continue (Y/n)?
用我的浏览器确认并登录后,令我惊讶的是它使用 StorageOptions.getDefaultInstance().getService();
所以我假设 GCP 从 application_default_credentials 文件而不是 GOOGLE_APPLICATION_CREDENTIALS 环境变量中获取凭据。
所以在我的本地环境中,它工作正常。问题是:这个网站部署在 Heroku 上,我不能使用 path 方法,因为我没有'不想将此凭据放在 GitHub 上。 我已经将环境变量放在 Heroku 并且没有工作。 也已经尝试使用这些构建包:
https://github.com/elishaterada/heroku-google-application-credentials-buildpack
https://github.com/gerywahyunugraha/heroku-google-application-credentials-buildpack
但是在 Heroku Cloud 上我无法让它工作。请问有人知道我可以使用12factor 规则运行此程序吗?
【问题讨论】:
标签: java spring spring-boot heroku google-cloud-storage