简体   繁体   English

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

[英]The Application Default Credentials are not available with environment variable setup in mac Google Cloud Storage

 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

Keep getting the above error instead I have set the environment variable on my local machine with the below command不断收到上述错误,而不是我使用以下命令在本地机器上设置了环境变量

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

If I check the path for the environment variable with the below command on terminal it does show the path of the variable如果我在终端上使用以下命令检查环境变量的路径,它会显示变量的路径

echo $GOOGLE_APPLICATION_CREDENTIALS

在此处输入图像描述

On Micronaut application I am trying to create a storage bucket during the startup在 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();
        }
    }
}

On the service在服务上

@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();
    }
}

The environment variable is NULL while running the application运行应用程序时环境变量为 NULL

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

The GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); causing an exception as导致异常为

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)

Is it on StartupEvent the micronaut doesn't access the environment variable?是在StartupEvent上 micronaut 没有访问环境变量吗?

Well I was missing the below instruction好吧,我错过了以下说明

Local development/testing本地开发/测试

If running locally for development/testing, you can use the Google Cloud SDK .如果在本地运行以进行开发/测试,您可以使用Google Cloud SDK Create Application Default Credentials with gcloud auth application-default login , and then google-cloud will automatically detect such credentials.使用gcloud auth application-default login创建应用程序默认凭据,然后 google-cloud 将自动检测此类凭据。

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

However, this solution is not perfect, since it is using the OAuth authentication and getting the warning as Your application has authenticated using end user credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your application continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error. For more information about service accounts.但是,此解决方案并不完美,因为它使用 OAuth 身份验证并收到警告,因为Your application has authenticated using end user credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your application continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error. For more information about service accounts. Your application has authenticated using end user credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your application continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error. For more information about service accounts.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Google Cloud Storage 不使用我的 GOOGLE_APPLICATION_CREDENTIALS 环境变量 [HEROKU]? - Why Google Cloud Storage isn't taking my GOOGLE_APPLICATION_CREDENTIALS environment variable [HEROKU]? Google Dialogflow:应用程序默认凭据不可用 - Google Dialogflow: The Application Default Credentials are not available Google Cloud Storage API无法在App Engine上获取应用程序默认凭据,以查找Compute Engine错误 - Google Cloud Storage API cannot get Application Default Credentials on App Engine looking for Compute Engine error 应用程序默认凭据不可用 - Application Default Credentials not available gcloud:云存储的附录中的默认应用程序凭据(java) - gcloud: Default Application Credentials in appengine for Cloud Storage (java) 如何使用 Google Cloud API - 应用程序默认凭据进行文本检测 - how to use Google Cloud API - Application Default Credentials for text detection 应用程序默认凭据不可用。 如果在 Google Compute Engine 中运行,它们就可用。 否则 - java android - The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise - java android Google Cloud Platform pub / sub Publisher,如何提供默认应用程序凭据以外的凭据 - Google Cloud Platform pub/sub Publisher, how to supply credentials other than default application credentials 向 Google Cloud Storage API 提供凭据 - Providing Credentials to Google Cloud Storage API 无法验证 google recaptcha 企业。 收到错误:java.io.IOException:应用程序默认凭据不可用 - Unable to validate google recaptcha enterprise. getting error: java.io.IOException: The Application Default Credentials are not available
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM