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

2023-12-07

 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"))

The GoogleCredentials credentials = 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)

是否开启StartupEventmicronaut无法访问环境变量?


好吧,我错过了以下说明

本地开发/测试

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

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

然而,这个解决方案并不完美,因为它使用 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.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐

  • 阻止 jquery-ui datepicker 在 beforeShow 中显示

    我想做这样的事情 var showme false mydatepicker datepicker beforeShow function input inst if showme stop showing 但禁用或隐藏似乎都不起作用 我想
  • 将 Power BI 连接到 S3 存储桶

    由于我是 Power BI 和 Redshift 的新手 需要一些指导 我的原始 JSON 数据以 gz 文件的形式存储在 Amazon S3 存储桶中 每个 gz 文件有多行 JSON 数据 我想将 Power BI 连接到 Amazon
  • Spring Batch Javaconfig - 参数化提交间隔又名块大小

    使用基于 Spring Batch xml 的配置 您可以参数化提交间隔 块大小 例如
  • 取消 Twitter 中待处理的 Bloodhound 请求 typeahead.js

    我的 Twitter typeahead js 设置如下 var filteredSource new Bloodhound datumTokenizer Bloodhound tokenizers obj whitespace value
  • WHERE 子句中的逻辑处理顺序或 SQL 标准

    几天前有人问我有关 SELECT 语句的逻辑处理顺序的问题 更具体地说 有关别名和 where 子句的问题 我不确定一个问题 如果我们有这样的查询 SELECT name AS first name FROM people WHERE fi
  • PhoneGap 中颜色选择器的示例代码

    我无法修复phonegap 中的colorpicker js 它不起作用 请帮忙吗 thanks colorSelector ColorPicker color 0000ff onShow function colpkr colpkr fa
  • 使用 AJAX 的文本内容和文件上传

    我正在为我的公司编写一个客户数据库系统 没有太多花哨的东西 但它做了它应该做的事情 现在所有基本的 文本 内容都已完成 我想在其中添加一些文件管理 我有几个表单通过ajax发送到后端 然后写入模型中的数据库 其中一些表格计划上传文档文件 有
  • 如何在时间列上组合基于约束的 R 数据帧

    我有两个 R 表 每个表都有一个用户列表和一个与他们采取特定操作的时间相对应的时间戳 其中第一个 df1 两个表具有详尽的用户列表 并且用户将具有具有不同时间戳的多行 第二 df2 将有一个更有限的用户列表 但用户将再次出现在表中 并具有不
  • 带有可编辑复选框的 JTable

    下面的代码是我的项目类之一 它生成一个框架JTable当我单击 查找 按钮时 一些数据将动态加载到表中 表的最后一列必须是带有事件的复选框 我尝试了这个复选框代码 从另一个项目中获取它 它不起作用 DefaultTableModel dtm
  • Perl 未在 Windows 10 中运行

    我刚刚在 64 位笔记本电脑上下载了适用于 Windows 10 的 ActivePerl 但是当我进入命令提示符时 perl v 会失败 除非目录是 C Perl64 bin 在这种情况下它会告诉我我有 Perl 5 20 2 版权所有
  • Kubernetes 部署 - 为图像拉取指定多个选项作为后备?

    我们所有可能的 docker 注册表 包括 Artifactory AWS ECR 和 GitLab 都曾一度遇到过镜像拉取问题 即使 DockerHub 偶尔也会出现问题 Kubernetes 部署中是否有一种方法可以指定 pod 可以从
  • 自定义运行时 node.js - 在 Google App Engine 中看不到我的自定义日志

    我们正在使用 Google App Engine 自定义运行时来运行移动应用程序的 Node js 服务器端代码 HTTP 请求日志记录工作正常 但我们的自定义日志存在问题 我们使用 Winston 和 log4js 作为日志记录框架 将应
  • jqPlot - 如何以编程方式查找哪个 jQuery UI 选项卡图存在

    请看下面的例子 http www jqplot com deploy dist examples hiddenPlotsInTabs html 在第一个示例中 通过捕获 tabshow 事件并查找选择了哪个选项卡来绘制隐藏图 tabs bi
  • 无法让 cabal 在 Windows 上找到 haskell-mpi 的 mpi 库 [关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 问题解决了 按照 Dons 发布的说明进行操作here
  • 如何创建包含单选和多选组合的完形填空题类型?

    我的问题出现在导出到 Moodle 的问题中 我打算创建一个完形填空题 其中一部分是单选 schoice 另一部分是多选 mchoice 使用 exams2moodle 函数导出时 我有两种可能的行为 所有问题都显示为 schoice 下拉
  • 如何使用 Ruby 对 iPhone 移动配置文件进行加密和签名

    我有一个由 iphone 配置实用程序生成的未签名的 xml 格式 模板 mobileconfig 文件 我想使用 openssl 对其进行加密和签名 并能够使用 Ruby on Rails 将其安装在 iphone 上 我不想创建 SCE
  • Pytorch maxpooling 通道维度

    我试图用 Pytorch 构建一个 cnn 但在 maxpooling 方面遇到了困难 我考了斯坦福举办的cs231n 我记得 maxpooling可以用作维度推导步骤 例如 我有这个 1 20 height width 输入ot max
  • C# 中的 64 位指针算术,检查算术溢出更改行为

    我有一些不安全的 C 代码 它们对类型上的大内存块进行指针算术byte 在 64 位机器上运行 它在大多数情况下都能正常工作 但是当事情变大时 我经常会遇到某种损坏 指针变得不正确 奇怪的是 如果我打开 检查算术溢出 下溢 一切都会正常工作
  • 正方体中未找到库错误[关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 我正在使用 OCR android 从谷歌搜索中获取样
  • 应用程序默认凭据不适用于 mac Google Cloud Storage 中的环境变量设置

    The Application Default Credentials are not available They are available if running in Google Compute Engine Otherwise t