javascript - TypeError: key 必须是一个字符串、一个缓冲区或一个在 typeError 中存在 GCP 文件的对象
问题描述
我试图简单地测试我们的谷歌云平台(GCP)存储上是否存在文件。我在 express js 服务器上使用 GCP 存储桶。下面本质上是一个非常简单的示例,复制自https://googleapis.dev/nodejs/storage/latest/File.html#exists
编辑:这就是我验证 GCP 密钥的方式:
const { Storage } = require('@google-cloud/storage');
const storage = new Storage({
projectId: 'my-cloud',
keyFilename: process.env.GOOGLE_APPLICATION_CREDENTIALS,
});
const bucketName = 'my-ci';
(稍作改动,我意识到你应该回来data[0]
)
const bucket = storage.bucket(bucketName);
const file = bucket.file(path);
const exists = await file.exists().then(data => {
return data
})
但是当我尝试运行它时,我得到了错误:
[nodemon] starting `node --inspect server/server.js`
Debugger listening on ws://127.0.0.1:9229/9a677766-4a93-4499-b57c-55f5f05096d7
For help, see: https://nodejs.org/en/docs/inspector
Server listening on port 4000!
/opt/node_modules/jwa/index.js:115
return new TypeError(errMsg);
^
TypeError: key must be a string, a buffer or an object
at typeError (/opt/node_modules/jwa/index.js:115:10)
at checkIsPrivateKey (/opt/node_modules/jwa/index.js:61:9)
at Object.sign (/opt/node_modules/jwa/index.js:147:5)
at Object.jwsSign [as sign] (/opt/node_modules/jws/lib/sign-stream.js:32:24)
at JWTAccess.getRequestHeaders (/opt/node_modules/google-auth-library/build/src/auth/jwtaccess.js:87:31)
at JWT.getRequestMetadataAsync (/opt/node_modules/google-auth-library/build/src/auth/jwtclient.js:76:51)
at JWT.getRequestHeaders (/opt/node_modules/google-auth-library/build/src/auth/oauth2client.js:238:37)
at GoogleAuth.authorizeRequest (/opt/node_modules/google-auth-library/build/src/auth/googleauth.js:593:38)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
由于错误消息没有给出有用的回溯,我自己做了一些挖掘。通过console.log
到处放置语句,我将范围缩小到行
const exists = await file.exists().then(data => {
return data
})
并尝试了各种方法,从删除.then(...)
子句到删除await
(在承诺解决之前确实有效)。这些似乎都没有奏效。
这可能是什么潜在原因?
解决方案
最终弄明白了——这是因为 GCP 密钥是旧版本的密钥。如果您收到上述错误,请尝试检查您的密钥是否正确。
推荐阅读
- php - PhP获取目录中所有文件的内容
- excel - 时间总和,转换为每小时升数 Excel 电子表格
- google-sheets - 从两个不同的单元格获取单元格值
- python - Ubuntu服务器中的文件排列不同
- angular - Angular Material Mat 子菜单点击
- javascript - Laravel Livewire:Foreach 循环中的组件。按钮单击显示所有项目而不是单个项目
- intellij-idea - 我们如何使用 Intellij ANTLR V4 插件测试 ANTLR Lexer 规则?
- javascript - 表单提交 - 覆盖默认提交操作并提示确认
- javascript - 尝试重定向到 Next JS API 路由器中的 url 给我一个 cors 问题
- linux-kernel - 内核:`什么会导致以前一直被读取的内存读取突然开始导致“无法处理内核分页请求”?