繁体   English   中英

尝试从 Firebase 存储加载图像时出现 403 禁止错误

[英]Getting 403 Forbidden error when trying to load image from Firebase Storage

我正在使用 firebase 存储为我的 android 应用程序上的用户存储和加载图像。 所有用户在使用之前都必须经过身份验证。 有时,某些用户个人资料图像未显示并引发“403 Forbidden”错误。 这些图像以前显示过,我不确定它们为什么停止工作。 我在我的 firebase 存储上使用以下规则:

 service firebase.storage {
   match /b/<storage_address>.appspot.com/o {
    match /{allPaths=**} {
     allow read: if request.auth != null;
     allow write: if request.auth != null;
    }
  }
}

如果我更改读取规则以allow read; 所有图像都正常工作。 这是我的显示方法:

Picasso.Builder builder = new Picasso.Builder(this);
                builder.listener(new Picasso.Listener()
                {
                    @Override
                    public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception)
                    {
                        exception.printStackTrace();
                    }
                });
                builder.build().load(URL).into(imgView);

图像 URL 看起来像这样:

https://firebasestorage.googleapis.com/v0/b/<storage_address>.appspot.com/o/images%2Fprofile%2Fphoto%2F36805?alt=media&token=e62ec151-3aaf-4e5c-aefb-1b3c93828684

这可能与令牌有关吗?

可能来得太晚了,但可能对其他人有帮助。 我遇到了同样的问题,我的问题是 firebase 存储中的文件名是唯一的。 如果您上传具有相同文件名的新文件,它将覆盖旧文件并为其生成新令牌,从而使旧 url 过时。 你应该为你上传的文件生成一个唯一的文件名,然后它应该被修复。

如果您只是在测试存储,则您可能没有实施身份验证。 默认情况下,Storage 要求对上传用户进行身份验证。 为了绕过它,在 Storage->Rules 中写下这个:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth == null;
    }
  }
}

对我来说就像一种魅力。 使用 Firebase 玩得开心

如果文件的扩展名不正确或丢失,您会收到以下错误:

 {
    "error": {
    "code": 403,
    "message": "Permission denied. Could not perform this operation"
    }
 }

在您提供的图像 URL 中,图像名称后没有图像扩展名。

我已经用我的一个网址进行了测试,这有效(您可以测试): https : //firebasestorage.googleapis.com/v0/b/training-a0a3e.appspot.com/o/penguin-56101_640.jpg?alt=media&token =8dfb913d-e2f3-4956-bd3a-2c3746d0d6d3

但是,当 .jpg 被删除时: https : //firebasestorage.googleapis.com/v0/b/training-a0a3e.appspot.com/o/penguin-56101_640? alt = media & token = 8dfb913d-e2f3-4956-bd3a-2c36d46d

收到以下响应:

 {
    "error": {
    "code": 403,
    "message": "Permission denied. Could not perform this operation"
    }
 }

授予每个人读取权限使我的应用程序再次正确显示图像:

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

可能这个不推荐,但是暂时解决了问题

我和你有同样的问题。 这是一个迟到的答案,但也许这可以帮助某人。

我的问题与user2555964描述的完全一样。

我可以看到您只提供了 url 并且它引用了一个令牌。 我假设您不会像这样从存储路径下载 url,而只是将引用保留在数据库中。

我的详细问题是,在上传图片时,我保存了 url 并将其保存在数据库中,然后我有一个存储触发器优化了 firebase 云功能(复制、优化和替换原始)上的图片,它改变了图片,在检查存储规则的身份验证时使 url 上的令牌毫无价值。

我的解决方案是存储存储路径,并使用正确的令牌下载当前 url。

为了增加这些现有的响应,我不幸遇到了一个用例,这里没有介绍。 假设您存储了由您选择的特殊字符分隔的两个 url,这就是您要做的:

var url = 'https://myUrl.jpg + https://anootherUrl.jpg' //This is a representation of the response you get from a query

String myUrl = url.toString().replaceAll("+", "").replaceAll("https://anotherUrl", "")//In Dart this is how to edit string objects. Removing unwanted parts by replacing with a blank

如果您选择的特殊字符是标准 url 格式的一部分,您将收到错误消息。 假设您使用了下划线(_),一些 url 路径包含下划线,因此通过将所有下划线替换为空白,路径将变为无效,从而引发 403 Forbidden 错误。 要解决它,只需检查 url 的格式并选择不在其中的分隔符(如有必要)。

Don't forget to update write permission

Please change the rule in google firebase console
Storage -> rules

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM