首页 > 解决方案 > 为什么我无法访问我的 Firebase 存储映像?

问题描述

我有两个图像文件上传到 Firebase 存储:

在此处输入图像描述

capsule house.jpg 是通过 UI 上传的(点击上传文件按钮)。

upload_64e8fd... 正在使用这个从我的后端服务器(node.js)上传:

const bucket = fbAdmin.storage().bucket('gs://assertivesolutions2.appspot.com');
const result = await bucket.upload(files.image.path);

capsule house.jps 被识别为 jpeg,并且在右侧空白处提供了指向它的链接。如果我单击它,我会在新选项卡中看到我的图像。你可以自己看看:

https://firebasestorage.googleapis.com/v0/b/assertivesolutions2.appspot.com/o/capsule%20house.jpg?alt=media&token=f5e0ccc4-7916-4245-b813-dbdf1838556f

upload_64e8fd... 不被识别为任何类型的图像文件,也没有提供链接。

后端返回的结果是一个巨大的 json 对象,包含以下字段:

"selfLink": "https://www.googleapis.com/storage/v1/b/assertivesolutions2.appspot.com/o/upload_64e8fd09f787acfe2728ae73158e20ab"
"mediaLink": "https://storage.googleapis.com/download/storage/v1/b/assertivesolutions2.appspot.com/o/upload_64e8fd09f787acfe2728ae73158e20ab?generation=1590547279565389&alt=media"

第一个将我发送到这样的页面:

{
  "error": {
    "code": 401,
    "message": "Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.",
    "errors": [
      {
        "message": "Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.",
        "domain": "global",
        "reason": "required",
        "locationType": "header",
        "location": "Authorization"
      }
    ]
  }
}

第二个给了我类似的东西:

Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.

我的存储桶的规则如下:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if true;
    }
  }
}

我允许所有的读写。

那么为什么它说当我的图像通过我的后端服务器上传时我无权查看我的图像?

我也想知道为什么当它通过我的后端服务器上传时它不会识别为 jpeg,但是当它通过 UI 上传时它会识别它,但我想关注这个问题的访问问题。

谢谢。

标签: node.jsgoogle-cloud-storagefirebase-storage

解决方案


默认情况下,文件作为私有文件上传,除非您更改存储桶设置,如此所述。以下代码是如何更改文档可见性的示例。

/**
 * {@inheritdoc}
 */
public function setVisibility($path, $visibility)
{
    $object = $this->getObject($path);

    if ($visibility === AdapterInterface::VISIBILITY_PRIVATE) {
        $object->acl()->delete('allUsers');
    } elseif ($visibility === AdapterInterface::VISIBILITY_PUBLIC) {
        $object->acl()->add('allUsers', Acl::ROLE_READER);
    }

    $normalised = $this->normaliseObject($object);
    $normalised['visibility'] = $visibility;

    return $normalised;
}

您可以按照官方文档中的教程检查如何通过控制台进行设置:公开数据

除此之外,正如@FrankvanPuffelen 的评论中所指出的那样,您将不会生成要访问的文件的 URL。您可以在此处找到有关它的更多信息。

让我知道这些信息是否对您有帮助!


推荐阅读