环境变量 GOOGLE_APPLICATION_CREDENTIALS 在 Google 云上仅持续一天

env variable GOOGLE_APPLICATION_CREDENTIALS last only one day on Google cloud

在 Google shell 中,它是 Google 云的一部分,我设置了环境变量 GOOGLE_APPLICATION_CREDENTIALS 因为 PHP NLP 项目需要它[信息:https://cloud.google.com/natural-language/docs/quickstart-client-libraries#client-libraries-install-php]。我的项目运行良好,但我注意到变量 GOOGLE_APPLICATION_CREDENTIALS 在我的系统上只持续了一天。这是我第三次设置它。当我缺少必需的变量时,我的项目无法运行。难道我做错了什么?

编辑: 当您在 Google App 引擎上创建新应用程序时,它是默认的 OS (Debian)。 当我在 Google shell 中输入帮助时,我得到的信息是: 您的 5GB 主目录将在会话中持续存在,但 VM 是临时的并且将被重置 会话结束后大约 20 分钟。除此之外,系统范围内的任何更改都不会持续存在。

假设您使用的是 Linux,请确保:

您完全正确,Cloud Shell 是 运行 在会话结束几分钟后重置的临时实例上,这就是您丢失所提到的环境变量内容的原因。

关于limitations in Cloud Shell的文档明确指出它仅供交互式使用,任何非交互式会话或密集使用都可以在有(或没有)警告的情况下自动终止。

因此,从您的问题中了解到您有一个正在使用 Cloud Natural Language, I would strongly advise you to move to a "real" instance of Compute Engine, in which you will have much more control about what is happening. This will allow more flexibility and you will be able to use a bigger machine type, given that Cloud Shell runs on a g1-small GCE instance which, in general, is not enough to run an application. Also, depending on your use case, you may even consider App Engine 的后台脚本。

也就是说,我发现在构造 LanguageClient 实例时,您也可以不使用应用程序默认凭据,而是使用 keyFilekeyFilePath 变量(在 the PHP Client Library reference 中解释)将 JSON 键的路径直接传递给您的代码,而不是从环境变量中读取它。