java - 应用程序默认凭据不适用于 mac Google Cloud Storage 中的环境变量设置

标签 java google-cloud-storage micronaut

 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

继续收到上述错误,而是我使用以下命令在本地计算机上设置了环境变量

export GOOGLE_APPLICATION_CREDENTIALS="/Users/macbook/Downloads/fetebird-2b6fa8261292.json"

如果我在终端上使用以下命令检查环境变量的路径,它会显示变量的路径

echo $GOOGLE_APPLICATION_CREDENTIALS

enter image description here

在 Micronaut 应用程序上,我尝试在启动期间创建一个存储桶

@Singleton
public class StartUp implements ApplicationEventListener<StartupEvent> {
    private final GoogleCloudStorageService googleCloudStorageService;

    public StartUp(GoogleCloudStorageService googleCloudStorageService) {
        this.googleCloudStorageService = googleCloudStorageService;
    }

    @Override
    public void onApplicationEvent(StartupEvent event) {
        try {
            this.googleCloudStorageService.createBucketWithStorageClassAndLocation().subscribe();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

关于服务

@Singleton
public record GoogleCloudStorageService(GoogleCloudStorageConfiguration googleUploadObjectConfiguration, GoogleCredentialsConfiguration googleCredentialsConfiguration) {

    private static final Logger LOG = LoggerFactory.getLogger(GoogleCloudStorageService.class);

    public Observable<Void> createBucketWithStorageClassAndLocation() throws IOException {
        GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); // fromStream(new FileInputStream(googleCredentialsConfiguration.getLocation()));
        Storage storage = StorageOptions.newBuilder().setCredentials(credentials).setProjectId(googleUploadObjectConfiguration.projectId()).build().getService();
        StorageClass storageClass = StorageClass.COLDLINE;
        try {
            Bucket bucket =
                    storage.create(
                            BucketInfo.newBuilder(googleUploadObjectConfiguration.bucketName())
                                    .setStorageClass(storageClass)
                                    .setLocation(googleUploadObjectConfiguration.locationName())
                                    .build());
            LOG.info(String.format("Created bucket %s in %s with storage class %s", bucket.getName(), bucket.getLocation(), bucket.getStorageClass()));
        } catch (Exception ex) {
            LOG.error(ex.getMessage());
        }
        return Observable.empty();
    }
}

运行应用程序时环境变量为NULL

System.out.println(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"))

GoogleCredentials credential = GoogleCredentials.getApplicationDefault(); 导致异常

java.io.IOException: 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.
    at com.google.auth.oauth2.DefaultCredentialsProvider.getDefaultCredentials(DefaultCredentialsProvider.java:134)
    at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:120)
    at com.google.auth.oauth2.GoogleCredentials.getApplicationDefault(GoogleCredentials.java:92)
    at fete.bird.service.gcp.GoogleCloudStorageService.createBucketWithStorageClassAndLocation(GoogleCloudStorageService.java:24)
    at fete.bird.core.StartUp.onApplicationEvent(StartUp.java:24)
    at fete.bird.core.StartUp.onApplicationEvent(StartUp.java:11)
    at io.micronaut.context.DefaultBeanContext.notifyEventListeners(DefaultBeanContext.java:1307)
    at io.micronaut.context.DefaultBeanContext.publishEvent(DefaultBeanContext.java:1292)
    at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:248)
    at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:166)
    at io.micronaut.runtime.Micronaut.start(Micronaut.java:71)
    at io.micronaut.runtime.Micronaut.run(Micronaut.java:311)
    at io.micronaut.runtime.Micronaut.run(Micronaut.java:297)
    at fete.bird.ServiceApplication.main(ServiceApplication.java:8)

是否在 StartupEvent 上,micronaut 没有访问环境变量?

最佳答案

我错过了以下说明

本地开发/测试

如果在本地运行以进行开发/测试,您可以使用 Google Cloud SDK。使用 gcloud auth application-default login 创建应用程序默认凭据,然后 google-cloud 将自动检测此类凭据。

https://github.com/googleapis/google-cloud-java

但是,此解决方案并不完美,因为它使用 OAuth 身份验证并收到警告:您的应用程序已使用 Google Cloud SDK 中的最终用户凭据进行身份验证。我们建议大多数服务器应用程序使用服务帐户。如果您的应用程序继续使用 Cloud SDK 中的最终用户凭据,您可能会收到“超出配额”或“API 未启用”错误。有关服务帐户的更多信息。

关于java - 应用程序默认凭据不适用于 mac Google Cloud Storage 中的环境变量设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66452523/

相关文章:

machine-learning - 将 jupyter Notebook 上传到 Google Cloud Datalab?

mongodb - java.lang.NoSuchFieldError : FSYNCED

java - 如何在 Java GUI 中的每个 TreeSelectionEvent 上刷新 JTable?

java - 为什么我们需要在实体中声明 NOT 空构造函数?

java - Hibernate - 不同的持久性方法

java - 模拟中时间管理的静态类

Google 云 Sql 上大于 1 TB 的 MySQL 表

google-cloud-storage - gsutil 失败 : [Errno 1] _ssl. c:504:错误:14090086:SSL 例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败

aws-lambda - 使用 DynamoDB 和 Graal 自定义运行时的 Micronaut 无服务器应用程序抛出无法构造 com.amazonaws.partitions.model.Partitions 的实例

redis - redis-lettuce 与 micronaut 集成时如何禁用 Redis 健康检查