我正在尝试使用file.getSignedUrl()从Firebase存储中通过Google Cloud Functions (Node.js)获取下载URL。在Cloud Functions控制台中,我遇到了以下错误:
{ SigningError: A Forbidden error was returned while attempting to retrieve an access token for the Compute Engine built-in service account. This may be because the Compute Engine instance does not have the correct permission scopes specified. Permission iam.serviceAccounts.signBlob is required to perform this operation on service account projects/myapp-cd94d/serviceAccounts/myapp-cd94d@appspot.gserviceaccount.com.
at SigningError (/user_code/node_modules/@google-cloud/storage/build/src/file.js:58:9)
at authClient.sign.then.catch.err (/user_code/node_modules/@google-cloud/storage/build/src/file.js:1019:22)
at process._tickDomainCallback (internal/process/next_tick.js:135:7) name: 'SigningError' }
我从将 Firebase Admin SDK 添加到您的服务器文档中复制了代码。 我在functions
文件夹中有我的serviceAccountKey.json
。 firebase deploy
没有给出错误。
Error parsing triggers: Cannot find module 'serviceAccountKey.json'
所以我必须找到正确的路径到我的serviceAccountKey.json
文件。我甚至生成了一个新的私钥,但这并没有解决问题。我使用的是firebase-admin 6.1.0
和firebase-tools 6.1.0
版本。以下是我的代码中相关的部分:
const admin = require('firebase-admin');
var serviceAccount = require("./myapp-cd94d-firebase-adminsdk-1234x-sEcReT.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://myapp-cd94d.firebaseio.com"
});
...
const config = {
action: 'read',
expires: '03-17-2025'
};
file.getSignedUrl(config).then(function(data) {
const url = data[0];
console.log(url);
})
.catch(function(error) {
console.error(error);
})
我看到 Doug Stevenson的回答 有不同的代码,但它似乎等效于文档中的代码。