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

lf3rwulv  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(263)

当我尝试在Blitz.js /api handler中使用@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'

但是当我在本地用node test_api.js运行它时,它工作得很好...
下面是我的代码:

// test_api.js

const {Storage} = require('@google-cloud/storage');

const client_id = process.env.GCP_STORAGE_ADMIN_CLIENT_ID
const projectId = process.env.GCP_PROJECT_ID
const client_email = process.env.GCP_STORAGE_ADMIN_CLIENT_EMAIL
const private_key = process.env.GCP_STORAGE_ADMIN_PRIVATE_KEY

const storage = new Storage({
    projectId,
    credentials: {
        client_id,
        client_email,
        private_key,
    }
});

async function listBuckets() {
    console.log('PRIVATE KEY: ', private_key) // the error seems to indicate there is an issue here
    // Output: "-----BEGIN PRIVATE KEY-----\n[the private key]\n-----END PRIVATE KEY-----\n"
    const [buckets] = await storage.getBuckets();
    console.log('Buckets:');
    buckets.forEach(bucket => {
      console.log(bucket.name);
    });
}

module.exports = {
    listBuckets // this function errors when called within Next.js /api handler
}

// When I uncomment this and run the file directly with node, it works
// listBuckets()

这个错误似乎表明我的私钥的开头/前缀有问题,但我完全是从Google服务帐户JSON文件中复制的。
-----BEGIN PRIVATE KEY-----\n[the private key]\n-----END PRIVATE KEY-----\n

7z5jn7bk

7z5jn7bk1#

结果是在我的.env.local环境变量中放错了一个尾随逗号。

GCP_STORAGE_ADMIN_PRIVATE_KEY="[the private key]",

后面的逗号并不明显,因为我关闭了自动换行,console.logs没有显示逗号,但它不知何故阅读了逗号,导致整个私钥解析不正确。

相关问题