在 Blitz.js api 处理程序中运行时,Google Cloud Storage 调用失败,并显示“错误:0909006C:PEM 例程:get_name:无起始行”

Mar*_*son 5 javascript node.js google-cloud-storage next.js

当我尝试在Blitz.js /api 处理程序中使用@google-cloud/storage时,它​​会生成以下错误:

error:0909006C:PEM routines:get_name:no start line
    at Sign.sign (internal/crypto/sig.js:110:29)
    at NodeCrypto.sign (C:\Users\markj\workspace\myapp\node_modules\google-auth-library\build\src\crypto\node\crypto.js:35:23)
    at GoogleAuth.sign (C:\Users\markj\workspace\myapp\node_modules\google-auth-library\build\src\auth\googleauth.js:561:39)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async sign (C:\Users\markj\workspace\myapp\node_modules\@google-cloud\storage\build\src\signer.js:174:35) {
  name: 'SigningError'
Run Code Online (Sandbox Code Playgroud)

但是,当我在本地运行它时node test_api.js,它工作正常......

这是我的代码:

error:0909006C:PEM routines:get_name:no start line
    at Sign.sign (internal/crypto/sig.js:110:29)
    at NodeCrypto.sign (C:\Users\markj\workspace\myapp\node_modules\google-auth-library\build\src\crypto\node\crypto.js:35:23)
    at GoogleAuth.sign (C:\Users\markj\workspace\myapp\node_modules\google-auth-library\build\src\auth\googleauth.js:561:39)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async sign (C:\Users\markj\workspace\myapp\node_modules\@google-cloud\storage\build\src\signer.js:174:35) {
  name: 'SigningError'
Run Code Online (Sandbox Code Playgroud)

该错误似乎表明我的私钥的开头/前缀有问题,但我从 Google 服务帐户 JSON 文件中准确复制了它。它看起来像这样:

-----BEGIN PRIVATE KEY-----\n[the private key]\n-----END PRIVATE KEY-----\n

Mar*_*son 6

事实证明,这是我的.env.local环境变量中放错位置的尾随逗号。

GCP_STORAGE_ADMIN_PRIVATE_KEY="[the private key]",
Run Code Online (Sandbox Code Playgroud)

尾随逗号并不明显,因为我关闭了自动换行,并且 console.logs 没有显示逗号,但它以某种方式读取逗号并导致整个私钥被错误解析。