首页 > 解决方案 > 尝试读取图像文件时,我不断从 Firebase 存储中收到 403

问题描述

我很难理解 Firebase 上传的整个令牌部分。

我想简单地上传使用头像,将它们保存到数据库中,然后在客户端读取它们。

const storageRef = firebase.storage().ref();
storageRef.child(`images/user-avatars/${user.uid}`).put(imageObj);

然后,在我的云函数中,我获取新的 url,如下所示:

exports.writeFileToDatabase = functions.storage.object().onFinalize(object => {
  const bucket = defaultStorage.bucket();
  const path = object.name as string;
  const file = bucket.file(path);

  return file
    .getSignedUrl({
      action: "read",
      expires: "03-17-2100"
    })
    .then(results => {
      const url = results[0];
      const silcedPath = path.split("/");

      return db
        .collection("venues")
        .doc(silcedPath[1])
        .set({ images: FieldValue.arrayUnion(url) }, { merge: true });
    });
});

我已IAM在 Google APIs 平台中启用,并已添加Cloud functions service agentApp Engine default service account.

我觉得以前的配置完全相同,但现在有时它甚至不写新的 url,或者我得到 403 试图读取它。对于我做错的事情,我找不到任何解释或错误。

编辑:忘记添加这段代码,但FieldValue在文档顶部设置为

const FieldValue = admin.firestore.FieldValue;

编辑:这是我得到的确切错误Failed to load resource: the server responded with a status of 403 ()

当我尝试使用这个由上面的函数自动生成的链接作为图像组件的来源时,我得到了它:

https://storage.googleapis.com/frothin-weirdos.appspot.com/images/user_avatars/yElCIVY4bAY5g5LnoOBhqN6mDNv2?GoogleAccessId=frothin-weirdos%40appspot.gserviceaccount.com&Expires=1742169600&Signature=qSqPuuY4c5xmdnpvfZh39Pw3Vyu2B%2FbGMD1rQwHDBUZTAnKwP11MaOFQt%2BTV53krkIgvJgQT0Xl3UUxkngmW9785fUri75SSPoBk0z4DKyZnEBLxgTGRE8MzmXadQ%2BHDJ3rSI8IkkoomdnANpLsPN9oySshZ1h4BfOBvAmK0hQ4Gge1glH7qhxFjVWfX3tovZoL8e2smhuCRXxDsZtJh0ihbIeZUEnX8lGic%2B9IT6y4OskS2ZlrZNjvM10hcEesoPdHsT4oCvfhCNbUcJcueRKfsWlDCd9m6qmf42WVOc7UI0nE0oEvysMutWY971GVRKTLwIXRnTLSNOr6fSvJE3Q%3D%3D

标签: javascriptfirebasegoogle-cloud-firestoregoogle-cloud-functionsfirebase-storage

解决方案


推荐阅读