javascript - 在 Blitz.js api 处理程序中运行时,Google Cloud Storage 调用失败并显示“错误:0909006C:PEM 例程:get_name:no start line”
问题描述
当我尝试在 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'
但是,当我在本地运行它时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
解决方案
原来是在我的.env.local
环境变量中放错了尾随逗号。
GCP_STORAGE_ADMIN_PRIVATE_KEY="[the private key]",
尾随逗号不明显,因为我关闭了自动换行,并且 console.logs 没有显示逗号,但它以某种方式读取了逗号并导致整个私钥被错误地解析。
推荐阅读
- python-3.x - Scapy 和 Docker:使用容器 ID 获取容器的 IP 地址?
- python - 使用 aiohttp 库登录网站
- php - Laravel 命名空间更改为 App\Models 后的包问题
- javascript - 需要 Javascript 信息
- typescript - @types/hapi__hapi 错误 TS2314:通用类型“侦听器”需要 1 个类型参数
- http - 在单页应用程序中定义动态内容安全策略
- winbugs - 为什么错误说模型需要左箭头或 WinBUGS 模型旋转?
- kubernetes - 如何在 k8s 中自定义资源?
- javascript - 在 Web 表单中将单选按钮呈现为可切换按钮的最佳方式
- html - Parcel-bundler 中的 Postcss 不起作用;没有使用 CSS 模块