我正在尝试简单地测试我们的 Google Cloud Platform (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 密钥是旧版本的密钥。如果您收到上述错误,请尝试检查您的密钥是否正确。