【问题标题】:How to use google-auth-library with Google Cloud Storage with NodeJS?如何通过 NodeJS 将 google-auth-library 与 Google Cloud Storage 一起使用?
【发布时间】:2019-05-12 16:15:43
【问题描述】:

我想在 NodeJS 中使用Google Cloud Storage,但使用google-auth-library 进行身份验证 具体来说:我想在heroku上托管它,所以我想将秘密保存在环境变量中,而不是文件中(因为我必须提交文件才能部署到heroku)。基本上是什么在 auth 库中建议: https://github.com/googleapis/google-auth-library-nodejs#loading-credentials-from-environment-variables

但我似乎无法将生成的客户端传递给 Storage 构造函数?

【问题讨论】:

    标签: node.js google-cloud-platform google-cloud-storage google-api-nodejs-client google-auth-library


    【解决方案1】:

    阅读代码 [1,2,3,4,5] 您应该能够将凭据作为构造函数选项传递:

    storageOptions = {
      projectId: 'your-project-id',
      credentials: {
            client_email: 'your-client-email',
            private_key: 'your-private-key'
      }
    };
    client = new Storage(storageOptions);
    

    【讨论】:

    • 谢谢,这就是我最终做的,但奇怪的是 2 个库不兼容,我希望使用 auth 库中的 fromJson 方法。
    • 这特别有趣,因为在底层存储库确实使用了 google-auth-library,它只是没有为您提供传递 google-auth-library 原语的方法。可能值得feature request
    • +1 对于这种方法,挖掘了 google-auth-library 代码和 @google-cloud 代码,不敢相信它们没有链接...... :(
    • 我还要补充一点,您需要做一些process.env.PRIVATE_KEY.replace(/\\n/gm, '\n') 才能获得格式正确的private_key
    • 刚刚写了一些东西 - medium.com/@sohilpandya/… 希望它对未来的人有所帮助。
    猜你喜欢
    • 2017-03-21
    • 1970-01-01
    • 2014-06-02
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 2015-08-28
    • 2015-09-07
    • 2021-07-20
    相关资源
    最近更新 更多