应用引擎项目访问谷歌云存储的默认凭据?

时间:2015-06-22 14:10:56

标签: google-app-engine google-cloud-storage

我有一个应用引擎项目,我想访问我的谷歌云存储默认存储桶。获取Credential对象的第一步失败,在实时实例上执行以下操作:

Credential creds = GoogleCredential.getApplicationDefault();

我收到的异常消息:

“应用程序默认凭据不可用。如果在Google Compute Engine中运行,则可以使用它们。否则,必须定义环境变量GOOGLE_APPLICATION_CREDENTIALS,指向定义凭据的文件。有关详细信息,请参阅https://developers.google.com/accounts/docs/application-default-credentials。”

此处的文档说生产应用引擎项目应该可以访问默认凭据吗?:

https://developers.google.com/identity/protocols/application-default-credentials

“3。如果您在Google App Engine生产中运行,将使用与该应用程序关联的内置服务帐户。”

我缺少一些设置吗?

我的应用引擎版本(gradle):

com.google.appengine:appengine-java-sdk:1.9.18

和谷歌云存储库版本:

com.google.apis:google-api-services-storage:v1-rev35-1.20.0

由于

1 个答案:

答案 0 :(得分:7)

Go to Google Developers Console -> Credentials and create a default service account .json key. When you do that, it will download a file like default-account-credentials.json for you. You must then put that file somewhere in your app engine project. This file will be referenced when you call

Credential creds = GoogleCredential.getApplicationDefault();

However, before that works you must set the environmental variable to that files location. To do that open up your appengine-web.xml file and put this line into it:

<env-variables>
            <env-var name="GOOGLE_APPLICATION_CREDENTIALS"
                value="WEB-INF/default-account-credentials.json" />
        </env-variables>

Where value is the path to your .json creds file you just downloaded.

Now it will work. (your gradle imports are fine)