gpt4 book ai didi

java - GAE 凭据在 App Engine 上不可用

转载 作者:行者123 更新时间:2023-12-02 00:18:35 24 4
gpt4 key购买 nike

我正在 Google App Engine 上运行一项服务,并且想要从 Cloud Storage Bucket 读取一些文件。

正如我在这里读到的那样 https://cloud.google.com/docs/authentication/production#obtaining_credentials_on_app_engine_standard_environment您可以隐式或显式获取凭据。

我正在运行时 java8 上运行该服务。

在我的服务中,我尝试过这两种方法,但都不起作用。

隐式方式

StorageOptions.getDefaultInstance().getService();

当我应用隐式方式时,我总是收到此错误

com.google.cloud.storage.StorageException: Anonymous caller does not have storage.buckets.get access to xxxxxxxxxx.
at com.google.cloud.storage.spi.v1.HttpStorageRpc.translate(HttpStorageRpc.java:229)
at com.google.cloud.storage.spi.v1.HttpStorageRpc.get(HttpStorageRpc.java:406)
at com.google.cloud.storage.StorageImpl$4.call(StorageImpl.java:215)
at com.google.cloud.storage.StorageImpl$4.call(StorageImpl.java:212)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:105)
at com.google.cloud.RetryHelper.run(RetryHelper.java:76)

显式方式

GoogleCredentials credentials = AppEngineCredentials.getApplicationDefault();
storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

通过应用 AppEngine 的显式方式,我收到此错误

The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.

我正在使用依赖项

    <dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.92.0</version>
</dependency>

在本地环境中运行是可行的。

提前致谢

最佳答案

在运行时 java8 上唯一适合我的代码是这个

import com.google.appengine.api.appidentity.AppIdentityService;
import com.google.appengine.api.appidentity.AppIdentityServiceFactory;
import com.google.auth.appengine.AppEngineCredentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.*;

AppIdentityService appIdentityService = AppIdentityServiceFactory.getAppIdentityService();
GoogleCredentials credentials = AppEngineCredentials.newBuilder().setAppIdentityService(appIdentityService).build();
storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();

关于java - GAE 凭据在 App Engine 上不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58082802/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com