为 AppEngine 柔性环境实例设置“GOOGLE_APPLICATION_CREDENTIALS”

Set `GOOGLE_APPLICATION_CREDENTIALS` for AppEngine Flexible Environment instances

我的应用程序需要设置 GOOGLE_APPLICATION_CREDENTIALS 环境变量。但是,当 SSH 进入我的 Google AppEngine Flexible Environment 实例和 运行 echo $GOOGLE_APPLICATION_CREDENTIALS 时,我发现环境变量未设置。

我正在通过 gcloud app deploy --image-url "gcr.io/$GCP_PROJECT_ID/$GITHUB_SHA" 进行部署。我还需要做些什么来确保 GOOGLE_APPLICATION_CREDENTIALS 在新的 运行 个实例上正确设置吗?

根据官方documentation

After you create an App Engine application, the App Engine default service account is created and used as the identity of the App Engine service. The App Engine default service account is associated with your Cloud project and executes tasks on behalf of your apps running in App Engine.

By default, the App Engine default service account has the Editor role in the project.

因此您不需要在 App Engine Flex 上为您的应用程序 运行 设置 GOOGLE_APPLICATION_CREDENTIALS。

自定义运行时 docker 图像是未由 google 处理或修改的图像,这意味着 GCloud 不可用,因为构建图像时未安装 sdk,如本文所述another question,自定义运行时也不包含任何凭据,仅灵活(基于 google 运行时)或标准实例包含默认凭据。

要解决此问题,您需要 create a service account 并添加必要的权限,创建此帐户后,您需要生成 json 凭据

要创建 json 凭据,必须访问 service accounts page

在菜单 select create key 模式 select [=38= 中,单击与您的服务帐户匹配的行中操作列中的 3 个点图标] 然后点击 CREATE 按钮。

需要将此文件添加到您的 docker 映像中,并且您需要使用凭据文件的路径定义环境变量 GOOGLE_APPLICATION_CREDENTIALS

例如,通过在 dockerfile

上添加环境变量
ENV GOOGLE_APPLICATION_CREDENTIALS="[PATH]"

通过这些步骤,您基于自定义运行时的 App Engine 实例将能够使用链接到服务帐户的凭据来使用其他 GCP 产品或服务