firebase - 使用 Firebase getSignedUrl() 进行签名错误

标签 firebase google-cloud-functions firebase-storage

我正在尝试使用 file.getSignedUrl()通过 Google Cloud Functions (Nodejs) 从 Firebase Storage 获取下载 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' }

我从 Add the Firebase Admin SDK to Your Server 复制了代码文档。我的 functions 文件夹中有我的 serviceAccountKey.jsonfirebase部署没有给我错误

Error parsing triggers: Cannot find module 'serviceAccountKey.json'

所以我必须拥有 serviceAccountKey.json 的正确路径。我什至生成了一个新的私钥,但这并没有解决问题。我有 firebase-admin 6.1.0firebase-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's answer有不同的代码,但它似乎与文档中的代码等效。

最佳答案

答案与Cloud Identity and Access Management有关。首先,转到您的 Google Cloud Platform IAM & admin页。您将看到各种服务帐户。查找类似 myapp-cd99d@appspot.gserviceaccount.com 的服务帐户。 Name 列中应显示 App Engine 默认服务帐户。 (如果错误消息引用了不同的服务帐户,请找到该服务帐户。)

角色列中,您可能会看到某些角色,也可能看不到。如果您收到 SigningError 消息,则 Role 列缺少 Service Account Token Creator 角色。选中 myapp-cd99d@appspot.gserviceaccount.com 左侧的复选框以选择服务帐户,然后单击右侧的铅笔进行编辑。在下一个屏幕中,点击+添加另一个角色。向下滚动到服务帐户,选择服务帐户 token 创建者,然后保存。现在,您应该在 App Engine 默认服务帐户Roles 列中看到 Service Account Token Creator。现在您有权创建签名 token 。

接下来,重复这些步骤并为Storage Object Creator添加角色。这将允许您运行 getSignedURL()

您可以保存或分配服务帐户管理员和存储管理员,其中分别包括服务帐户 token 创建者存储对象创建者角色,以及其他角色。

现在,如果您收到 SingingError 消息,可能是因为您在唱 Bruce Springsteen 的“Glory Days”时跑调了。 :-)

关于firebase - 使用 Firebase getSignedUrl() 进行签名错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53305784/

相关文章:

firebase - 在 Firebase 客户端应用程序中实现可调用的云函数

java - 图像未从 Firebase 存储加载

swift - Firebase 查询未被触发

node.js - Firebase 功能部署失败

Android Firebase 远程配置初始提取不返回值

node.js - 更新 CloudFunctions 上的子集合

ios - 使用缓存中的图像创建图像数组 (Swift)

java - 使用 Android 时,项目似乎无法上传到 Firebase 实时数据库

swift - 无法将类型 '(User?, Error?) -> ()' 的值转换为预期的参数类型 'AuthDataResultCallback?'

android - Firestore - 如何在数据库中自动递减整数值?