如何将 google-auth-library 与 Google Cloud Storage 和 NodeJS 结合使用?

Pan*_*Man 2 node.js google-cloud-storage google-api-nodejs-client google-cloud-platform google-auth-library

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

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

Dav*_*vid 6

阅读代码 [ 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);
Run Code Online (Sandbox Code Playgroud)