node.js - 在 403 ("The project to be billed is associated with an absent billing account"中访问 firebase 存储中的文件)

标签 node.js google-cloud-functions firebase-storage

我正在开发一个 Firebase Cloud Function,它从存储在 Firebase 存储中的文件中收集元数据

我的代码大致是这样的:

const { Storage } = require('@google-cloud/storage');
const storage = new Storage();

exports.someFunction = functions.https.onCall((data, context) => {
    getMetadata("myBucket", data.filename)
        .then(() => {
            console.log("OK");
            return;
        })
        .catch(err => {
            console.error(err);
        })

    // do some other stuff here ....

    return;
}

async function getMetadata(bucketName, filename) {
    const [metadata] = await storage
        .bucket(bucketName)
        .file(filename)
        .getMetadata();  <<== ERROR HAPPENS IN THIS CALL

    console.log(metadata.selfLink);
    console.log(metadata.size);
}

每当 storage.bucket(bucketName).file(filename).getMetadata() 被调用时,如上面的代码所示,它会导致 403 与“The project to be billed is associated缺少计费帐户。”我试过在本地(使用 firebase functions:shell)和部署运行它。这是完整的错误消息:

{ Error: The project to be billed is associated with an absent billing account.
    at Util.parseHttpRespBody (/srv/node_modules/@google-cloud/common/build/src/util.js:191:38)
    at Util.handleResp (/srv/node_modules/@google-cloud/common/build/src/util.js:135:117)
    at retryRequest (/srv/node_modules/@google-cloud/common/build/src/util.js:423:22)
    at onResponse (/srv/node_modules/retry-request/index.js:200:7)
    at /srv/node_modules/teeny-request/build/src/index.js:222:13
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)
  code: 403,
  errors: 
   [ { domain: 'global',
       reason: 'accountDisabled',
       message: 'The project to be billed is associated with an absent billing account.',
       locationType: 'header',
       location: 'Authorization' } ],
  response: undefined,
  message: 'The project to be billed is associated with an absent billing account.' }

那个 Firebase 项目已经实现了几周的“Blaze 计划”,计费帐户已经启用了大约一年,其他 GCP 项目也被收费。

我读过服务帐户应该具有“signBlob”权限,我通过将“服务帐户 token 创建者”添加到默认 AppEngine 服务帐户和 Firebsae 服务帐户来尝试。

我还尝试通过 Firebase 的管理 SDK 初始化存储

const serviceAccount = require("path-to-json-downloaded-from-firebase-console.json");
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://<my-project>.firebaseio.com"
});
const storage = admin.storage();

但两者都不走运。

我无法弄清楚哪个帐户被“禁用”或“不存在”,也不知道我应该配置什么才能让它运行并卡住。

任何可以让我进一步排除故障的指示都会有很大帮助。

最佳答案

原因是我误解了代码调用中的“bucket”和“filename”。

在我的例子中,输入 bucketName 作为 gs://[my-project].appspot.com 和 fileName 作为存储桶下的完整路径解决了这个问题。

更明确地说,参数应该是这样的:

const [metadata] = await storage
        .bucket("gs://[my-project].appspot.com")
        .file("path1/path2/actual-file-name")
        .getMetadata();

关于node.js - 在 403 ("The project to be billed is associated with an absent billing account"中访问 firebase 存储中的文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55998315/

相关文章:

javascript - 使用异步/等待延迟强制请求之间的时间

javascript - 在 NodeJS 中转义多个嵌套函数

google-maps - 启用我的 Node.js Web 应用程序来保存谷歌地图图片

node.js - windows下用nodejs抓包

node.js - 实时数据库功能不断触发

android - Firebase:从数据库中引用存储中的图像

javascript - Firebase 函数 - 如何验证从 CDN 缓存加载静态内容?

firebase - 将文件从 Firebase Cloud Functions 上传到 Cloud Storage

android - 使用 URL 从 Firebase 存储中删除文件

angular - 如何使用 Angularfire2 从 Firebase 存储中删除