【发布时间】:2021-02-25 18:36:57
【问题描述】:
当我尝试在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
【问题讨论】:
标签: javascript node.js google-cloud-storage next.js